PR binutils/13257
[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,
8616ad89 3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
1b09e940 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.
252b5132
RH
107*/
108
109/* Summary of archive member names:
110
111 Symbol table (must be first):
112 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
113 "/ " - Symbol table, system 5 style.
114
115 Long name table (must be before regular file members):
116 "// " - Long name table, System 5 R4 style.
117 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
118
119 Regular file members with short names:
120 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
121 "filename.o " - Regular file, Berkeley style (no embedded spaces).
122
123 Regular files with long names (or embedded spaces, for BSD variants):
124 "/18 " - SVR4 style, name at offset 18 in name table.
390c0e42 125 "#1/23 " - Long name (or embedded spaces) 23 characters long,
252b5132 126 BSD 4.4 style, full name follows header.
252b5132
RH
127 " 18 " - Long name 18 characters long, extended pseudo-BSD.
128 */
129
252b5132 130#include "sysdep.h"
3db64b00 131#include "bfd.h"
b820f1e4 132#include "libiberty.h"
252b5132
RH
133#include "libbfd.h"
134#include "aout/ar.h"
135#include "aout/ranlib.h"
3882b010 136#include "safe-ctype.h"
109f7096 137#include "hashtab.h"
a8da6403 138#include "filenames.h"
252b5132
RH
139
140#ifndef errno
141extern int errno;
142#endif
143
252b5132
RH
144/* We keep a cache of archive filepointers to archive elements to
145 speed up searching the archive by filepos. We only add an entry to
146 the cache when we actually read one. We also don't sort the cache;
147 it's generally short enough to search linearly.
148 Note that the pointers here point to the front of the ar_hdr, not
047066e1
KH
149 to the front of the contents! */
150struct ar_cache {
252b5132 151 file_ptr ptr;
109f7096 152 bfd *arbfd;
252b5132
RH
153};
154
155#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
156#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
157
beb0d161 158#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
a8da6403 159#define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
8f95b6e4
TG
160
161/* True iff NAME designated a BSD 4.4 extended name. */
162
163#define is_bsd44_extended_name(NAME) \
164 (NAME[0] == '#' && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
390c0e42
JJ
165\f
166void
167_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
168{
169 static char buf[20];
170 size_t len;
171 snprintf (buf, sizeof (buf), fmt, val);
172 len = strlen (buf);
173 if (len < n)
174 {
175 memcpy (p, buf, len);
176 memset (p + len, ' ', n - len);
177 }
178 else
179 memcpy (p, buf, n);
180}
252b5132 181\f
b34976b6 182bfd_boolean
c58b9523 183_bfd_generic_mkarchive (bfd *abfd)
252b5132 184{
dc810e39 185 bfd_size_type amt = sizeof (struct artdata);
252b5132 186
a50b1753 187 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
252b5132 188 if (bfd_ardata (abfd) == NULL)
b34976b6 189 return FALSE;
252b5132 190
9e492e05
JJ
191 /* Already cleared by bfd_zalloc above.
192 bfd_ardata (abfd)->cache = NULL;
193 bfd_ardata (abfd)->archive_head = NULL;
194 bfd_ardata (abfd)->symdefs = NULL;
195 bfd_ardata (abfd)->extended_names = NULL;
196 bfd_ardata (abfd)->extended_names_size = 0;
197 bfd_ardata (abfd)->tdata = NULL; */
252b5132 198
b34976b6 199 return TRUE;
252b5132
RH
200}
201
202/*
203FUNCTION
204 bfd_get_next_mapent
205
206SYNOPSIS
c58b9523
AM
207 symindex bfd_get_next_mapent
208 (bfd *abfd, symindex previous, carsym **sym);
252b5132
RH
209
210DESCRIPTION
211 Step through archive @var{abfd}'s symbol table (if it
212 has one). Successively update @var{sym} with the next symbol's
213 information, returning that symbol's (internal) index into the
214 symbol table.
215
216 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
217 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
218 got the last one.
219
220 A <<carsym>> is a canonical archive symbol. The only
221 user-visible element is its name, a null-terminated string.
222*/
223
224symindex
c58b9523 225bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
252b5132
RH
226{
227 if (!bfd_has_map (abfd))
228 {
229 bfd_set_error (bfd_error_invalid_operation);
230 return BFD_NO_MORE_SYMBOLS;
231 }
232
233 if (prev == BFD_NO_MORE_SYMBOLS)
234 prev = 0;
235 else
236 ++prev;
237 if (prev >= bfd_ardata (abfd)->symdef_count)
238 return BFD_NO_MORE_SYMBOLS;
239
240 *entry = (bfd_ardata (abfd)->symdefs + prev);
241 return prev;
242}
243
06fc8a8c 244/* To be called by backends only. */
252b5132
RH
245
246bfd *
c58b9523 247_bfd_create_empty_archive_element_shell (bfd *obfd)
252b5132
RH
248{
249 return _bfd_new_bfd_contained_in (obfd);
250}
251
252/*
253FUNCTION
254 bfd_set_archive_head
255
256SYNOPSIS
c58b9523 257 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
252b5132
RH
258
259DESCRIPTION
260 Set the head of the chain of
261 BFDs contained in the archive @var{output} to @var{new_head}.
262*/
263
b34976b6 264bfd_boolean
c58b9523 265bfd_set_archive_head (bfd *output_archive, bfd *new_head)
252b5132 266{
252b5132 267 output_archive->archive_head = new_head;
b34976b6 268 return TRUE;
252b5132
RH
269}
270
271bfd *
c58b9523 272_bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
252b5132 273{
a2633f4e 274 htab_t hash_table = bfd_ardata (arch_bfd)->cache;
109f7096
BE
275 struct ar_cache m;
276 m.ptr = filepos;
252b5132 277
109f7096
BE
278 if (hash_table)
279 {
280 struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
281 if (!entry)
282 return NULL;
283 else
284 return entry->arbfd;
285 }
286 else
287 return NULL;
288}
289
290static hashval_t
291hash_file_ptr (const PTR p)
292{
293 return (hashval_t) (((struct ar_cache *) p)->ptr);
294}
252b5132 295
109f7096
BE
296/* Returns non-zero if P1 and P2 are equal. */
297
298static int
299eq_file_ptr (const PTR p1, const PTR p2)
300{
301 struct ar_cache *arc1 = (struct ar_cache *) p1;
302 struct ar_cache *arc2 = (struct ar_cache *) p2;
303 return arc1->ptr == arc2->ptr;
252b5132
RH
304}
305
9fcd9da6
NC
306/* The calloc function doesn't always take size_t (e.g. on VMS)
307 so wrap it to avoid a compile time warning. */
308
309static void *
310_bfd_calloc_wrapper (size_t a, size_t b)
311{
312 return calloc (a, b);
313}
314
06fc8a8c
NC
315/* Kind of stupid to call cons for each one, but we don't do too many. */
316
b34976b6 317bfd_boolean
c58b9523 318_bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
252b5132 319{
109f7096
BE
320 struct ar_cache *cache;
321 htab_t hash_table = bfd_ardata (arch_bfd)->cache;
252b5132 322
109f7096
BE
323 /* If the hash table hasn't been created, create it. */
324 if (hash_table == NULL)
252b5132 325 {
109f7096 326 hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
9fcd9da6 327 NULL, _bfd_calloc_wrapper, free);
109f7096
BE
328 if (hash_table == NULL)
329 return FALSE;
330 bfd_ardata (arch_bfd)->cache = hash_table;
252b5132
RH
331 }
332
109f7096 333 /* Insert new_elt into the hash table by filepos. */
a50b1753 334 cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
109f7096
BE
335 cache->ptr = filepos;
336 cache->arbfd = new_elt;
337 *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
338
b34976b6 339 return TRUE;
252b5132
RH
340}
341\f
a8da6403
NC
342static bfd *
343_bfd_find_nested_archive (bfd *arch_bfd, const char *filename)
344{
345 bfd *abfd;
98c53ba3 346 const char *target;
a8da6403
NC
347
348 for (abfd = arch_bfd->nested_archives;
349 abfd != NULL;
350 abfd = abfd->archive_next)
351 {
007d6189 352 if (filename_cmp (filename, abfd->filename) == 0)
a8da6403
NC
353 return abfd;
354 }
98c53ba3
AM
355 target = NULL;
356 if (!arch_bfd->target_defaulted)
357 target = arch_bfd->xvec->name;
358 abfd = bfd_openr (filename, target);
a8da6403
NC
359 if (abfd)
360 {
361 abfd->archive_next = arch_bfd->nested_archives;
362 arch_bfd->nested_archives = abfd;
363 }
364 return abfd;
365}
366
252b5132 367/* The name begins with space. Hence the rest of the name is an index into
d70910e8 368 the string table. */
252b5132
RH
369
370static char *
a8da6403 371get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
252b5132 372{
91d6fa6a 373 unsigned long table_index = 0;
a8da6403 374 const char *endp;
252b5132
RH
375
376 /* Should extract string so that I can guarantee not to overflow into
d70910e8 377 the next region, but I'm too lazy. */
252b5132 378 errno = 0;
d70910e8 379 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
91d6fa6a
NC
380 table_index = strtol (name + 1, (char **) &endp, 10);
381 if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
252b5132
RH
382 {
383 bfd_set_error (bfd_error_malformed_archive);
384 return NULL;
385 }
a8da6403
NC
386 /* In a thin archive, a member of an archive-within-an-archive
387 will have the offset in the inner archive encoded here. */
388 if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
389 {
390 file_ptr origin = strtol (endp + 1, NULL, 10);
391
4ad4f3cb 392 if (errno != 0)
a8da6403
NC
393 {
394 bfd_set_error (bfd_error_malformed_archive);
395 return NULL;
396 }
397 *originp = origin;
398 }
399 else
400 *originp = 0;
252b5132 401
91d6fa6a 402 return bfd_ardata (arch)->extended_names + table_index;
252b5132
RH
403}
404
405/* This functions reads an arch header and returns an areltdata pointer, or
406 NULL on error.
407
408 Presumes the file pointer is already in the right place (ie pointing
409 to the ar_hdr in the file). Moves the file pointer; on success it
410 should be pointing to the front of the file contents; on failure it
06fc8a8c 411 could have been moved arbitrarily. */
252b5132 412
c58b9523
AM
413void *
414_bfd_generic_read_ar_hdr (bfd *abfd)
252b5132 415{
c58b9523 416 return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
252b5132
RH
417}
418
419/* Alpha ECOFF uses an optional different ARFMAG value, so we have a
420 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
421
c58b9523
AM
422void *
423_bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
252b5132
RH
424{
425 struct ar_hdr hdr;
426 char *hdrp = (char *) &hdr;
dc810e39 427 size_t parsed_size;
252b5132
RH
428 struct areltdata *ared;
429 char *filename = NULL;
dc810e39
AM
430 bfd_size_type namelen = 0;
431 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
252b5132 432 char *allocptr = 0;
a8da6403 433 file_ptr origin = 0;
8f95b6e4 434 unsigned int extra_size = 0;
252b5132 435
c58b9523 436 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
252b5132
RH
437 {
438 if (bfd_get_error () != bfd_error_system_call)
439 bfd_set_error (bfd_error_no_more_archived_files);
440 return NULL;
441 }
442 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
443 && (mag == NULL
444 || strncmp (hdr.ar_fmag, mag, 2) != 0))
445 {
446 bfd_set_error (bfd_error_malformed_archive);
447 return NULL;
448 }
449
450 errno = 0;
451 parsed_size = strtol (hdr.ar_size, NULL, 10);
452 if (errno != 0)
453 {
454 bfd_set_error (bfd_error_malformed_archive);
455 return NULL;
456 }
457
458 /* Extract the filename from the archive - there are two ways to
459 specify an extended name table, either the first char of the
460 name is a space, or it's a slash. */
461 if ((hdr.ar_name[0] == '/'
462 || (hdr.ar_name[0] == ' '
463 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
464 && bfd_ardata (abfd)->extended_names != NULL)
465 {
a8da6403 466 filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
252b5132 467 if (filename == NULL)
9e492e05 468 return NULL;
252b5132 469 }
8f95b6e4
TG
470 /* BSD4.4-style long filename. */
471 else if (is_bsd44_extended_name (hdr.ar_name))
252b5132
RH
472 {
473 /* BSD-4.4 extended name */
474 namelen = atoi (&hdr.ar_name[3]);
475 allocsize += namelen + 1;
476 parsed_size -= namelen;
8f95b6e4 477 extra_size = namelen;
252b5132 478
a50b1753 479 allocptr = (char *) bfd_zalloc (abfd, allocsize);
252b5132
RH
480 if (allocptr == NULL)
481 return NULL;
482 filename = (allocptr
483 + sizeof (struct areltdata)
484 + sizeof (struct ar_hdr));
dc810e39 485 if (bfd_bread (filename, namelen, abfd) != namelen)
252b5132
RH
486 {
487 if (bfd_get_error () != bfd_error_system_call)
488 bfd_set_error (bfd_error_no_more_archived_files);
489 return NULL;
490 }
491 filename[namelen] = '\0';
492 }
493 else
494 {
495 /* We judge the end of the name by looking for '/' or ' '.
496 Note: The SYSV format (terminated by '/') allows embedded
d70910e8 497 spaces, so only look for ' ' if we don't find '/'. */
252b5132
RH
498
499 char *e;
a50b1753 500 e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
252b5132
RH
501 if (e == NULL)
502 {
a50b1753 503 e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
252b5132 504 if (e == NULL)
a50b1753 505 e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
252b5132
RH
506 }
507
508 if (e != NULL)
509 namelen = e - hdr.ar_name;
510 else
511 {
512 /* If we didn't find a termination character, then the name
513 must be the entire field. */
514 namelen = ar_maxnamelen (abfd);
515 }
516
517 allocsize += namelen + 1;
518 }
519
520 if (!allocptr)
521 {
a50b1753 522 allocptr = (char *) bfd_zalloc (abfd, allocsize);
252b5132
RH
523 if (allocptr == NULL)
524 return NULL;
525 }
526
527 ared = (struct areltdata *) allocptr;
528
529 ared->arch_header = allocptr + sizeof (struct areltdata);
c58b9523 530 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
252b5132 531 ared->parsed_size = parsed_size;
8f95b6e4 532 ared->extra_size = extra_size;
a8da6403 533 ared->origin = origin;
252b5132
RH
534
535 if (filename != NULL)
536 ared->filename = filename;
537 else
538 {
539 ared->filename = allocptr + (sizeof (struct areltdata) +
540 sizeof (struct ar_hdr));
541 if (namelen)
c58b9523 542 memcpy (ared->filename, hdr.ar_name, namelen);
252b5132
RH
543 ared->filename[namelen] = '\0';
544 }
545
c58b9523 546 return ared;
252b5132
RH
547}
548\f
a8da6403
NC
549/* Append the relative pathname for a member of the thin archive
550 to the pathname of the directory containing the archive. */
551
4b544b64
TG
552char *
553_bfd_append_relative_path (bfd *arch, char *elt_name)
a8da6403
NC
554{
555 const char *arch_name = arch->filename;
556 const char *base_name = lbasename (arch_name);
557 size_t prefix_len;
558 char *filename;
559
560 if (base_name == arch_name)
561 return elt_name;
562
563 prefix_len = base_name - arch_name;
a50b1753 564 filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
a8da6403
NC
565 if (filename == NULL)
566 return NULL;
567
568 strncpy (filename, arch_name, prefix_len);
569 strcpy (filename + prefix_len, elt_name);
570 return filename;
571}
572
252b5132
RH
573/* This is an internal function; it's mainly used when indexing
574 through the archive symbol table, but also used to get the next
575 element, since it handles the bookkeeping so nicely for us. */
576
577bfd *
c58b9523 578_bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
252b5132
RH
579{
580 struct areltdata *new_areldata;
581 bfd *n_nfd;
a8da6403 582 char *filename;
252b5132 583
1b09e940
NC
584 if (archive->my_archive)
585 {
586 filepos += archive->origin;
587 archive = archive->my_archive;
588 }
589
252b5132
RH
590 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
591 if (n_nfd)
592 return n_nfd;
593
594 if (0 > bfd_seek (archive, filepos, SEEK_SET))
595 return NULL;
596
a50b1753 597 if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
252b5132
RH
598 return NULL;
599
a8da6403
NC
600 filename = new_areldata->filename;
601
602 if (bfd_is_thin_archive (archive))
603 {
98c53ba3
AM
604 const char *target;
605
a8da6403
NC
606 /* This is a proxy entry for an external file. */
607 if (! IS_ABSOLUTE_PATH (filename))
608 {
4b544b64 609 filename = _bfd_append_relative_path (archive, filename);
a8da6403
NC
610 if (filename == NULL)
611 return NULL;
612 }
613
614 if (new_areldata->origin > 0)
615 {
616 /* This proxy entry refers to an element of a nested archive.
617 Locate the member of that archive and return a bfd for it. */
618 bfd *ext_arch = _bfd_find_nested_archive (archive, filename);
619
620 if (ext_arch == NULL
621 || ! bfd_check_format (ext_arch, bfd_archive))
622 {
623 bfd_release (archive, new_areldata);
624 return NULL;
625 }
626 n_nfd = _bfd_get_elt_at_filepos (ext_arch, new_areldata->origin);
627 if (n_nfd == NULL)
628 {
629 bfd_release (archive, new_areldata);
630 return NULL;
631 }
632 n_nfd->proxy_origin = bfd_tell (archive);
633 return n_nfd;
634 }
635 /* It's not an element of a nested archive;
636 open the external file as a bfd. */
98c53ba3
AM
637 target = NULL;
638 if (!archive->target_defaulted)
639 target = archive->xvec->name;
640 n_nfd = bfd_openr (filename, target);
c4948609
NC
641 if (n_nfd == NULL)
642 bfd_set_error (bfd_error_malformed_archive);
a8da6403
NC
643 }
644 else
645 {
646 n_nfd = _bfd_create_empty_archive_element_shell (archive);
647 }
648
252b5132
RH
649 if (n_nfd == NULL)
650 {
c58b9523 651 bfd_release (archive, new_areldata);
252b5132
RH
652 return NULL;
653 }
654
a8da6403
NC
655 n_nfd->proxy_origin = bfd_tell (archive);
656
657 if (bfd_is_thin_archive (archive))
658 {
659 n_nfd->origin = 0;
660 }
661 else
662 {
663 n_nfd->origin = n_nfd->proxy_origin;
664 n_nfd->filename = filename;
665 }
666
c58b9523 667 n_nfd->arelt_data = new_areldata;
252b5132 668
e326bf5f
L
669 /* Copy BFD_COMPRESS and BFD_DECOMPRESS flags. */
670 n_nfd->flags |= archive->flags & (BFD_COMPRESS | BFD_DECOMPRESS);
671
252b5132
RH
672 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
673 return n_nfd;
674
c58b9523 675 bfd_release (archive, new_areldata);
252b5132
RH
676 return NULL;
677}
678
679/* Return the BFD which is referenced by the symbol in ABFD indexed by
91d6fa6a 680 SYM_INDEX. SYM_INDEX should have been returned by bfd_get_next_mapent. */
252b5132
RH
681
682bfd *
91d6fa6a 683_bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
252b5132
RH
684{
685 carsym *entry;
686
91d6fa6a 687 entry = bfd_ardata (abfd)->symdefs + sym_index;
252b5132
RH
688 return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
689}
690
691/*
692FUNCTION
693 bfd_openr_next_archived_file
694
695SYNOPSIS
c58b9523 696 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
252b5132
RH
697
698DESCRIPTION
699 Provided a BFD, @var{archive}, containing an archive and NULL, open
700 an input BFD on the first contained element and returns that.
701 Subsequent calls should pass
702 the archive and the previous return value to return a created
703 BFD to the next contained element. NULL is returned when there
704 are no more.
252b5132
RH
705*/
706
707bfd *
c58b9523 708bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
252b5132 709{
a8da6403
NC
710 if ((bfd_get_format (archive) != bfd_archive)
711 || (archive->direction == write_direction))
252b5132
RH
712 {
713 bfd_set_error (bfd_error_invalid_operation);
714 return NULL;
715 }
716
e326bf5f 717 return BFD_SEND (archive,
c58b9523 718 openr_next_archived_file, (archive, last_file));
252b5132
RH
719}
720
721bfd *
c58b9523 722bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
252b5132
RH
723{
724 file_ptr filestart;
725
726 if (!last_file)
727 filestart = bfd_ardata (archive)->first_file_filepos;
728 else
729 {
730 unsigned int size = arelt_size (last_file);
c4948609 731
a8da6403
NC
732 filestart = last_file->proxy_origin;
733 if (! bfd_is_thin_archive (archive))
734 filestart += size;
1b09e940
NC
735 if (archive->my_archive)
736 filestart -= archive->origin;
252b5132
RH
737 /* Pad to an even boundary...
738 Note that last_file->origin can be odd in the case of
d70910e8 739 BSD-4.4-style element with a long odd size. */
252b5132
RH
740 filestart += filestart % 2;
741 }
742
743 return _bfd_get_elt_at_filepos (archive, filestart);
744}
745
252b5132 746const bfd_target *
c58b9523 747bfd_generic_archive_p (bfd *abfd)
252b5132
RH
748{
749 struct artdata *tdata_hold;
750 char armag[SARMAG + 1];
dc810e39 751 bfd_size_type amt;
252b5132 752
c58b9523 753 if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
252b5132
RH
754 {
755 if (bfd_get_error () != bfd_error_system_call)
756 bfd_set_error (bfd_error_wrong_format);
757 return NULL;
758 }
759
a8da6403
NC
760 bfd_is_thin_archive (abfd) = (strncmp (armag, ARMAGT, SARMAG) == 0);
761
762 if (strncmp (armag, ARMAG, SARMAG) != 0
763 && strncmp (armag, ARMAGB, SARMAG) != 0
764 && ! bfd_is_thin_archive (abfd))
c4948609 765 return NULL;
252b5132 766
487e54f2 767 tdata_hold = bfd_ardata (abfd);
252b5132 768
487e54f2 769 amt = sizeof (struct artdata);
a50b1753 770 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
252b5132 771 if (bfd_ardata (abfd) == NULL)
487e54f2
AM
772 {
773 bfd_ardata (abfd) = tdata_hold;
774 return NULL;
775 }
252b5132
RH
776
777 bfd_ardata (abfd)->first_file_filepos = SARMAG;
9e492e05
JJ
778 /* Cleared by bfd_zalloc above.
779 bfd_ardata (abfd)->cache = NULL;
780 bfd_ardata (abfd)->archive_head = NULL;
781 bfd_ardata (abfd)->symdefs = NULL;
782 bfd_ardata (abfd)->extended_names = NULL;
783 bfd_ardata (abfd)->extended_names_size = 0;
784 bfd_ardata (abfd)->tdata = NULL; */
252b5132 785
487e54f2
AM
786 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
787 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
252b5132 788 {
252b5132
RH
789 if (bfd_get_error () != bfd_error_system_call)
790 bfd_set_error (bfd_error_wrong_format);
252b5132 791 bfd_release (abfd, bfd_ardata (abfd));
487e54f2 792 bfd_ardata (abfd) = tdata_hold;
252b5132
RH
793 return NULL;
794 }
795
796 if (bfd_has_map (abfd))
797 {
798 bfd *first;
799
800 /* This archive has a map, so we may presume that the contents
801 are object files. Make sure that if the first file in the
802 archive can be recognized as an object file, it is for this
803 target. If not, assume that this is the wrong format. If
804 the first file is not an object file, somebody is doing
805 something weird, and we permit it so that ar -t will work.
806
807 This is done because any normal format will recognize any
808 normal archive, regardless of the format of the object files.
809 We do accept an empty archive. */
810
c58b9523 811 first = bfd_openr_next_archived_file (abfd, NULL);
252b5132
RH
812 if (first != NULL)
813 {
b34976b6 814 first->target_defaulted = FALSE;
252b5132
RH
815 if (bfd_check_format (first, bfd_object)
816 && first->xvec != abfd->xvec)
817 {
3619ad04 818 bfd_set_error (bfd_error_wrong_object_format);
487e54f2 819 bfd_ardata (abfd) = tdata_hold;
252b5132
RH
820 return NULL;
821 }
3619ad04 822 /* And we ought to close `first' here too. */
252b5132
RH
823 }
824 }
825
826 return abfd->xvec;
827}
828
829/* Some constants for a 32 bit BSD archive structure. We do not
830 support 64 bit archives presently; so far as I know, none actually
831 exist. Supporting them would require changing these constants, and
dc810e39 832 changing some H_GET_32 to H_GET_64. */
252b5132
RH
833
834/* The size of an external symdef structure. */
835#define BSD_SYMDEF_SIZE 8
836
837/* The offset from the start of a symdef structure to the file offset. */
838#define BSD_SYMDEF_OFFSET_SIZE 4
839
840/* The size of the symdef count. */
841#define BSD_SYMDEF_COUNT_SIZE 4
842
843/* The size of the string count. */
844#define BSD_STRING_COUNT_SIZE 4
845
36e44abd
AN
846/* Read a BSD-style archive symbol table. Returns FALSE on error,
847 TRUE otherwise. */
252b5132 848
b34976b6 849static bfd_boolean
c58b9523 850do_slurp_bsd_armap (bfd *abfd)
252b5132
RH
851{
852 struct areltdata *mapdata;
853 unsigned int counter;
854 bfd_byte *raw_armap, *rbase;
855 struct artdata *ardata = bfd_ardata (abfd);
856 char *stringbase;
dc810e39 857 bfd_size_type parsed_size, amt;
252b5132
RH
858 carsym *set;
859
a50b1753 860 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
252b5132 861 if (mapdata == NULL)
b34976b6 862 return FALSE;
252b5132 863 parsed_size = mapdata->parsed_size;
c58b9523 864 bfd_release (abfd, mapdata); /* Don't need it any more. */
252b5132 865
a50b1753 866 raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
c58b9523 867 if (raw_armap == NULL)
b34976b6 868 return FALSE;
252b5132 869
c58b9523 870 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
252b5132
RH
871 {
872 if (bfd_get_error () != bfd_error_system_call)
873 bfd_set_error (bfd_error_malformed_archive);
874 byebye:
c58b9523 875 bfd_release (abfd, raw_armap);
b34976b6 876 return FALSE;
252b5132
RH
877 }
878
dc810e39 879 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
252b5132
RH
880
881 if (ardata->symdef_count * BSD_SYMDEF_SIZE >
882 parsed_size - BSD_SYMDEF_COUNT_SIZE)
883 {
884 /* Probably we're using the wrong byte ordering. */
885 bfd_set_error (bfd_error_wrong_format);
886 goto byebye;
887 }
888
889 ardata->cache = 0;
890 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
891 stringbase = ((char *) rbase
892 + ardata->symdef_count * BSD_SYMDEF_SIZE
893 + BSD_STRING_COUNT_SIZE);
c58b9523 894 amt = ardata->symdef_count * sizeof (carsym);
a50b1753 895 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
252b5132 896 if (!ardata->symdefs)
b34976b6 897 return FALSE;
252b5132
RH
898
899 for (counter = 0, set = ardata->symdefs;
900 counter < ardata->symdef_count;
901 counter++, set++, rbase += BSD_SYMDEF_SIZE)
902 {
dc810e39
AM
903 set->name = H_GET_32 (abfd, rbase) + stringbase;
904 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
905 }
906
907 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 908 /* Pad to an even boundary if you have to. */
252b5132
RH
909 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
910 /* FIXME, we should provide some way to free raw_ardata when
911 we are done using the strings from it. For now, it seems
d70910e8 912 to be allocated on an objalloc anyway... */
b34976b6
AM
913 bfd_has_map (abfd) = TRUE;
914 return TRUE;
252b5132
RH
915}
916
36e44abd
AN
917/* Read a COFF archive symbol table. Returns FALSE on error, TRUE
918 otherwise. */
047066e1 919
b34976b6 920static bfd_boolean
c58b9523 921do_slurp_coff_armap (bfd *abfd)
252b5132
RH
922{
923 struct areltdata *mapdata;
924 int *raw_armap, *rawptr;
925 struct artdata *ardata = bfd_ardata (abfd);
926 char *stringbase;
dc810e39 927 bfd_size_type stringsize;
252b5132
RH
928 unsigned int parsed_size;
929 carsym *carsyms;
dc810e39 930 bfd_size_type nsymz; /* Number of symbols in armap. */
edeb6e24 931 bfd_vma (*swap) (const void *);
252b5132 932 char int_buf[sizeof (long)];
dc810e39
AM
933 bfd_size_type carsym_size, ptrsize;
934 unsigned int i;
252b5132 935
a50b1753 936 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
252b5132 937 if (mapdata == NULL)
b34976b6 938 return FALSE;
252b5132 939 parsed_size = mapdata->parsed_size;
c58b9523 940 bfd_release (abfd, mapdata); /* Don't need it any more. */
252b5132 941
c58b9523 942 if (bfd_bread (int_buf, 4, abfd) != 4)
252b5132
RH
943 {
944 if (bfd_get_error () != bfd_error_system_call)
945 bfd_set_error (bfd_error_malformed_archive);
b34976b6 946 return FALSE;
252b5132
RH
947 }
948 /* It seems that all numeric information in a coff archive is always
d70910e8 949 in big endian format, nomatter the host or target. */
252b5132 950 swap = bfd_getb32;
c58b9523 951 nsymz = bfd_getb32 (int_buf);
252b5132
RH
952 stringsize = parsed_size - (4 * nsymz) - 4;
953
252b5132
RH
954 /* ... except that some archive formats are broken, and it may be our
955 fault - the i960 little endian coff sometimes has big and sometimes
956 little, because our tools changed. Here's a horrible hack to clean
957 up the crap. */
958
959 if (stringsize > 0xfffff
960 && bfd_get_arch (abfd) == bfd_arch_i960
961 && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
962 {
047066e1 963 /* This looks dangerous, let's do it the other way around. */
c58b9523 964 nsymz = bfd_getl32 (int_buf);
252b5132
RH
965 stringsize = parsed_size - (4 * nsymz) - 4;
966 swap = bfd_getl32;
967 }
252b5132
RH
968
969 /* The coff armap must be read sequentially. So we construct a
d70910e8 970 bsd-style one in core all at once, for simplicity. */
252b5132 971
933d961a
JJ
972 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
973 return FALSE;
974
252b5132
RH
975 carsym_size = (nsymz * sizeof (carsym));
976 ptrsize = (4 * nsymz);
977
933d961a
JJ
978 if (carsym_size + stringsize + 1 <= carsym_size)
979 return FALSE;
980
a50b1753
NC
981 ardata->symdefs = (struct carsym *) bfd_zalloc (abfd,
982 carsym_size + stringsize + 1);
252b5132 983 if (ardata->symdefs == NULL)
b34976b6 984 return FALSE;
252b5132
RH
985 carsyms = ardata->symdefs;
986 stringbase = ((char *) ardata->symdefs) + carsym_size;
987
d70910e8 988 /* Allocate and read in the raw offsets. */
a50b1753 989 raw_armap = (int *) bfd_alloc (abfd, ptrsize);
252b5132
RH
990 if (raw_armap == NULL)
991 goto release_symdefs;
c58b9523
AM
992 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
993 || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
252b5132
RH
994 {
995 if (bfd_get_error () != bfd_error_system_call)
996 bfd_set_error (bfd_error_malformed_archive);
997 goto release_raw_armap;
998 }
999
047066e1 1000 /* OK, build the carsyms. */
252b5132
RH
1001 for (i = 0; i < nsymz; i++)
1002 {
1003 rawptr = raw_armap + i;
c58b9523 1004 carsyms->file_offset = swap ((bfd_byte *) rawptr);
252b5132
RH
1005 carsyms->name = stringbase;
1006 stringbase += strlen (stringbase) + 1;
1007 carsyms++;
1008 }
1009 *stringbase = 0;
1010
1011 ardata->symdef_count = nsymz;
1012 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 1013 /* Pad to an even boundary if you have to. */
252b5132
RH
1014 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1015
b34976b6 1016 bfd_has_map (abfd) = TRUE;
c58b9523 1017 bfd_release (abfd, raw_armap);
252b5132 1018
047066e1 1019 /* Check for a second archive header (as used by PE). */
252b5132
RH
1020 {
1021 struct areltdata *tmp;
1022
dc810e39 1023 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
a50b1753 1024 tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
23afc6f6 1025 if (tmp != NULL)
252b5132
RH
1026 {
1027 if (tmp->arch_header[0] == '/'
23afc6f6 1028 && tmp->arch_header[1] == ' ')
252b5132
RH
1029 {
1030 ardata->first_file_filepos +=
dc810e39 1031 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
252b5132
RH
1032 }
1033 bfd_release (abfd, tmp);
1034 }
1035 }
1036
b34976b6 1037 return TRUE;
252b5132
RH
1038
1039release_raw_armap:
c58b9523 1040 bfd_release (abfd, raw_armap);
252b5132 1041release_symdefs:
c58b9523 1042 bfd_release (abfd, (ardata)->symdefs);
b34976b6 1043 return FALSE;
252b5132
RH
1044}
1045
36e44abd
AN
1046/* This routine can handle either coff-style or bsd-style armaps
1047 (archive symbol table). Returns FALSE on error, TRUE otherwise */
252b5132 1048
b34976b6 1049bfd_boolean
c58b9523 1050bfd_slurp_armap (bfd *abfd)
252b5132
RH
1051{
1052 char nextname[17];
c58b9523 1053 int i = bfd_bread (nextname, 16, abfd);
252b5132
RH
1054
1055 if (i == 0)
b34976b6 1056 return TRUE;
252b5132 1057 if (i != 16)
b34976b6 1058 return FALSE;
252b5132 1059
dc810e39 1060 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 1061 return FALSE;
252b5132 1062
0112cd26
NC
1063 if (CONST_STRNEQ (nextname, "__.SYMDEF ")
1064 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
252b5132 1065 return do_slurp_bsd_armap (abfd);
0112cd26 1066 else if (CONST_STRNEQ (nextname, "/ "))
252b5132 1067 return do_slurp_coff_armap (abfd);
0112cd26 1068 else if (CONST_STRNEQ (nextname, "/SYM64/ "))
252b5132 1069 {
36b45482
TS
1070 /* 64bit ELF (Irix 6) archive. */
1071#ifdef BFD64
c58b9523 1072 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
36b45482
TS
1073 return bfd_elf64_archive_slurp_armap (abfd);
1074#else
252b5132 1075 bfd_set_error (bfd_error_wrong_format);
b34976b6 1076 return FALSE;
36b45482 1077#endif
252b5132 1078 }
cba0723b
TG
1079 else if (CONST_STRNEQ (nextname, "#1/20 "))
1080 {
1081 /* Mach-O has a special name for armap when the map is sorted by name.
1082 However because this name has a space it is slightly more difficult
1083 to check it. */
1084 struct ar_hdr hdr;
1085 char extname[21];
1086
1087 if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1088 return FALSE;
1089 /* Read the extended name. We know its length. */
1090 if (bfd_bread (extname, 20, abfd) != 20)
1091 return FALSE;
1092 if (bfd_seek (abfd, (file_ptr) -(sizeof (hdr) + 20), SEEK_CUR) != 0)
1093 return FALSE;
8f95b6e4
TG
1094 if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
1095 || CONST_STRNEQ (extname, "__.SYMDEF"))
cba0723b
TG
1096 return do_slurp_bsd_armap (abfd);
1097 }
252b5132 1098
b34976b6
AM
1099 bfd_has_map (abfd) = FALSE;
1100 return TRUE;
252b5132
RH
1101}
1102\f
06fc8a8c
NC
1103/* Returns FALSE on error, TRUE otherwise. */
1104/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
252b5132 1105 header is in a slightly different order and the map name is '/'.
d70910e8 1106 This flavour is used by hp300hpux. */
252b5132
RH
1107
1108#define HPUX_SYMDEF_COUNT_SIZE 2
1109
b34976b6 1110bfd_boolean
c58b9523 1111bfd_slurp_bsd_armap_f2 (bfd *abfd)
252b5132
RH
1112{
1113 struct areltdata *mapdata;
1114 char nextname[17];
1115 unsigned int counter;
1116 bfd_byte *raw_armap, *rbase;
1117 struct artdata *ardata = bfd_ardata (abfd);
1118 char *stringbase;
1119 unsigned int stringsize;
8616ad89 1120 unsigned int left;
dc810e39 1121 bfd_size_type amt;
252b5132 1122 carsym *set;
c58b9523 1123 int i = bfd_bread (nextname, 16, abfd);
252b5132
RH
1124
1125 if (i == 0)
b34976b6 1126 return TRUE;
252b5132 1127 if (i != 16)
b34976b6 1128 return FALSE;
252b5132 1129
047066e1 1130 /* The archive has at least 16 bytes in it. */
dc810e39 1131 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 1132 return FALSE;
252b5132 1133
0112cd26
NC
1134 if (CONST_STRNEQ (nextname, "__.SYMDEF ")
1135 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
252b5132
RH
1136 return do_slurp_bsd_armap (abfd);
1137
0112cd26 1138 if (! CONST_STRNEQ (nextname, "/ "))
252b5132 1139 {
b34976b6
AM
1140 bfd_has_map (abfd) = FALSE;
1141 return TRUE;
252b5132
RH
1142 }
1143
a50b1753 1144 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
252b5132 1145 if (mapdata == NULL)
b34976b6 1146 return FALSE;
252b5132 1147
8616ad89 1148 if (mapdata->parsed_size < HPUX_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
252b5132 1149 {
8616ad89
AM
1150 wrong_format:
1151 bfd_set_error (bfd_error_wrong_format);
252b5132 1152 byebye:
c58b9523 1153 bfd_release (abfd, mapdata);
b34976b6 1154 return FALSE;
252b5132 1155 }
8616ad89
AM
1156 left = mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE - BSD_STRING_COUNT_SIZE;
1157
1158 amt = mapdata->parsed_size;
1159 raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt);
1160 if (raw_armap == NULL)
1161 goto byebye;
252b5132 1162
c58b9523 1163 if (bfd_bread (raw_armap, amt, abfd) != amt)
252b5132
RH
1164 {
1165 if (bfd_get_error () != bfd_error_system_call)
1166 bfd_set_error (bfd_error_malformed_archive);
252b5132
RH
1167 goto byebye;
1168 }
1169
c58b9523 1170 ardata->symdef_count = H_GET_16 (abfd, raw_armap);
252b5132 1171
252b5132
RH
1172 ardata->cache = 0;
1173
dc810e39 1174 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
8616ad89
AM
1175 if (stringsize > left)
1176 goto wrong_format;
1177 left -= stringsize;
1178
047066e1 1179 /* Skip sym count and string sz. */
252b5132
RH
1180 stringbase = ((char *) raw_armap
1181 + HPUX_SYMDEF_COUNT_SIZE
1182 + BSD_STRING_COUNT_SIZE);
1183 rbase = (bfd_byte *) stringbase + stringsize;
c58b9523 1184 amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
8616ad89
AM
1185 if (amt > left)
1186 goto wrong_format;
1187
a50b1753 1188 ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
252b5132 1189 if (!ardata->symdefs)
b34976b6 1190 return FALSE;
252b5132
RH
1191
1192 for (counter = 0, set = ardata->symdefs;
1193 counter < ardata->symdef_count;
1194 counter++, set++, rbase += BSD_SYMDEF_SIZE)
1195 {
dc810e39
AM
1196 set->name = H_GET_32 (abfd, rbase) + stringbase;
1197 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
1198 }
1199
1200 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 1201 /* Pad to an even boundary if you have to. */
252b5132
RH
1202 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1203 /* FIXME, we should provide some way to free raw_ardata when
1204 we are done using the strings from it. For now, it seems
d70910e8 1205 to be allocated on an objalloc anyway... */
b34976b6
AM
1206 bfd_has_map (abfd) = TRUE;
1207 return TRUE;
252b5132
RH
1208}
1209\f
1210/** Extended name table.
1211
1212 Normally archives support only 14-character filenames.
1213
1214 Intel has extended the format: longer names are stored in a special
1215 element (the first in the archive, or second if there is an armap);
1216 the name in the ar_hdr is replaced by <space><index into filename
1217 element>. Index is the P.R. of an int (decimal). Data General have
047066e1
KH
1218 extended the format by using the prefix // for the special element. */
1219
b34976b6 1220/* Returns FALSE on error, TRUE otherwise. */
252b5132 1221
b34976b6 1222bfd_boolean
c58b9523 1223_bfd_slurp_extended_name_table (bfd *abfd)
252b5132
RH
1224{
1225 char nextname[17];
1226 struct areltdata *namedata;
dc810e39 1227 bfd_size_type amt;
252b5132
RH
1228
1229 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
b34976b6 1230 we probably don't want to return TRUE. */
eb00922a
MS
1231 if (bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET) != 0)
1232 return FALSE;
1233
c58b9523 1234 if (bfd_bread (nextname, 16, abfd) == 16)
252b5132 1235 {
dc810e39 1236 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 1237 return FALSE;
252b5132 1238
0112cd26
NC
1239 if (! CONST_STRNEQ (nextname, "ARFILENAMES/ ")
1240 && ! CONST_STRNEQ (nextname, "// "))
252b5132
RH
1241 {
1242 bfd_ardata (abfd)->extended_names = NULL;
9e492e05 1243 bfd_ardata (abfd)->extended_names_size = 0;
b34976b6 1244 return TRUE;
252b5132
RH
1245 }
1246
a50b1753 1247 namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
252b5132 1248 if (namedata == NULL)
b34976b6 1249 return FALSE;
252b5132 1250
dc810e39 1251 amt = namedata->parsed_size;
9e492e05
JJ
1252 if (amt + 1 == 0)
1253 goto byebye;
1254
1255 bfd_ardata (abfd)->extended_names_size = amt;
a50b1753 1256 bfd_ardata (abfd)->extended_names = (char *) bfd_zalloc (abfd, amt + 1);
252b5132
RH
1257 if (bfd_ardata (abfd)->extended_names == NULL)
1258 {
1259 byebye:
c58b9523 1260 bfd_release (abfd, namedata);
b34976b6 1261 return FALSE;
252b5132
RH
1262 }
1263
c58b9523 1264 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
252b5132
RH
1265 {
1266 if (bfd_get_error () != bfd_error_system_call)
1267 bfd_set_error (bfd_error_malformed_archive);
c58b9523 1268 bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
252b5132
RH
1269 bfd_ardata (abfd)->extended_names = NULL;
1270 goto byebye;
1271 }
1272
1273 /* Since the archive is supposed to be printable if it contains
1274 text, the entries in the list are newline-padded, not null
1275 padded. In SVR4-style archives, the names also have a
1276 trailing '/'. DOS/NT created archive often have \ in them
1277 We'll fix all problems here.. */
1278 {
9e492e05
JJ
1279 char *ext_names = bfd_ardata (abfd)->extended_names;
1280 char *temp = ext_names;
252b5132 1281 char *limit = temp + namedata->parsed_size;
047066e1
KH
1282 for (; temp < limit; ++temp)
1283 {
89a58aeb 1284 if (*temp == ARFMAG[1])
9e492e05 1285 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
047066e1
KH
1286 if (*temp == '\\')
1287 *temp = '/';
1288 }
9e492e05 1289 *limit = '\0';
252b5132
RH
1290 }
1291
047066e1 1292 /* Pad to an even boundary if you have to. */
252b5132
RH
1293 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1294 bfd_ardata (abfd)->first_file_filepos +=
1295 (bfd_ardata (abfd)->first_file_filepos) % 2;
1296
1297 /* FIXME, we can't release namedata here because it was allocated
d70910e8 1298 below extended_names on the objalloc... */
252b5132 1299 }
b34976b6 1300 return TRUE;
252b5132
RH
1301}
1302
1303#ifdef VMS
1304
1305/* Return a copy of the stuff in the filename between any :]> and a
047066e1
KH
1306 semicolon. */
1307
252b5132 1308static const char *
c58b9523 1309normalize (bfd *abfd, const char *file)
252b5132 1310{
dc810e39
AM
1311 const char *first;
1312 const char *last;
252b5132
RH
1313 char *copy;
1314
1315 first = file + strlen (file) - 1;
1316 last = first + 1;
1317
1318 while (first != file)
1319 {
1320 if (*first == ';')
1321 last = first;
1322 if (*first == ':' || *first == ']' || *first == '>')
1323 {
1324 first++;
1325 break;
1326 }
1327 first--;
1328 }
1329
c58b9523 1330 copy = bfd_alloc (abfd, last - first + 1);
252b5132
RH
1331 if (copy == NULL)
1332 return NULL;
1333
1334 memcpy (copy, first, last - first);
1335 copy[last - first] = 0;
1336
1337 return copy;
1338}
1339
1340#else
1341static const char *
c58b9523 1342normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
252b5132 1343{
19172f39 1344 return lbasename (file);
252b5132
RH
1345}
1346#endif
1347
c4948609
NC
1348/* Adjust a relative path name based on the reference path.
1349 For example:
1350
1351 Relative path Reference path Result
1352 ------------- -------------- ------
1353 bar.o lib.a bar.o
1354 foo/bar.o lib.a foo/bar.o
1355 bar.o foo/lib.a ../bar.o
1356 foo/bar.o baz/lib.a ../foo/bar.o
1357 bar.o ../lib.a <parent of current dir>/bar.o
74ce8de7
NC
1358 ; ../bar.o ../lib.a bar.o
1359 ; ../bar.o lib.a ../bar.o
c4948609
NC
1360 foo/bar.o ../lib.a <parent of current dir>/foo/bar.o
1361 bar.o ../../lib.a <grandparent>/<parent>/bar.o
1362 bar.o foo/baz/lib.a ../../bar.o
1363
74ce8de7
NC
1364 Note - the semicolons above are there to prevent the BFD chew
1365 utility from interpreting those lines as prototypes to put into
1366 the autogenerated bfd.h header...
a8da6403 1367
74ce8de7
NC
1368 Note - the string is returned in a static buffer. */
1369
a8da6403
NC
1370static const char *
1371adjust_relative_path (const char * path, const char * ref_path)
1372{
1373 static char *pathbuf = NULL;
c4948609
NC
1374 static unsigned int pathbuf_len = 0;
1375 const char *pathp;
1376 const char *refp;
1377 char * lpath;
1378 char * rpath;
1379 unsigned int len;
1380 unsigned int dir_up = 0;
1381 unsigned int dir_down = 0;
a8da6403 1382 char *newp;
c4948609
NC
1383 char * pwd = getpwd ();
1384 const char * down;
a8da6403 1385
c4948609
NC
1386 /* Remove symlinks, '.' and '..' from the paths, if possible. */
1387 lpath = lrealpath (path);
1388 pathp = lpath == NULL ? path : lpath;
1389
1390 rpath = lrealpath (ref_path);
1391 refp = rpath == NULL ? ref_path : rpath;
1392
a8da6403
NC
1393 /* Remove common leading path elements. */
1394 for (;;)
1395 {
1396 const char *e1 = pathp;
1397 const char *e2 = refp;
1398
1399 while (*e1 && ! IS_DIR_SEPARATOR (*e1))
1400 ++e1;
1401 while (*e2 && ! IS_DIR_SEPARATOR (*e2))
1402 ++e2;
1403 if (*e1 == '\0' || *e2 == '\0' || e1 - pathp != e2 - refp
007d6189 1404 || filename_ncmp (pathp, refp, e1 - pathp) != 0)
a8da6403
NC
1405 break;
1406 pathp = e1 + 1;
1407 refp = e2 + 1;
1408 }
1409
c4948609 1410 len = strlen (pathp) + 1;
a8da6403
NC
1411 /* For each leading path element in the reference path,
1412 insert "../" into the path. */
1413 for (; *refp; ++refp)
1414 if (IS_DIR_SEPARATOR (*refp))
c4948609
NC
1415 {
1416 /* PR 12710: If the path element is "../" then instead of
1417 inserting "../" we need to insert the name of the directory
1418 at the current level. */
1419 if (refp > ref_path + 1
1420 && refp[-1] == '.'
1421 && refp[-2] == '.')
1422 dir_down ++;
1423 else
1424 dir_up ++;
1425 }
1426
1427 /* If the lrealpath calls above succeeded then we should never
1428 see dir_up and dir_down both being non-zero. */
1429
1430 len += 3 * dir_up;
1431
1432 if (dir_down)
1433 {
1434 down = pwd + strlen (pwd) - 1;
1435
1436 while (dir_down && down > pwd)
1437 {
1438 if (IS_DIR_SEPARATOR (*down))
1439 --dir_down;
1440 }
1441 BFD_ASSERT (dir_down == 0);
1442 len += strlen (down) + 1;
1443 }
1444 else
1445 down = NULL;
a8da6403
NC
1446
1447 if (len > pathbuf_len)
1448 {
1449 if (pathbuf != NULL)
1450 free (pathbuf);
1451 pathbuf_len = 0;
a50b1753 1452 pathbuf = (char *) bfd_malloc (len);
a8da6403 1453 if (pathbuf == NULL)
c4948609 1454 goto out;
a8da6403
NC
1455 pathbuf_len = len;
1456 }
1457
1458 newp = pathbuf;
c4948609 1459 while (dir_up-- > 0)
a8da6403
NC
1460 {
1461 /* FIXME: Support Windows style path separators as well. */
1462 strcpy (newp, "../");
1463 newp += 3;
1464 }
a8da6403 1465
c4948609
NC
1466 if (down)
1467 sprintf (newp, "%s/%s", down, pathp);
1468 else
1469 strcpy (newp, pathp);
1470
1471 out:
1472 free (lpath);
1473 free (rpath);
a8da6403
NC
1474 return pathbuf;
1475}
1476
252b5132
RH
1477/* Build a BFD style extended name table. */
1478
b34976b6 1479bfd_boolean
c58b9523
AM
1480_bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1481 char **tabloc,
1482 bfd_size_type *tablen,
1483 const char **name)
252b5132
RH
1484{
1485 *name = "ARFILENAMES/";
b34976b6 1486 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
252b5132
RH
1487}
1488
1489/* Build an SVR4 style extended name table. */
1490
b34976b6 1491bfd_boolean
c58b9523
AM
1492_bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1493 char **tabloc,
1494 bfd_size_type *tablen,
1495 const char **name)
252b5132
RH
1496{
1497 *name = "//";
b34976b6 1498 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
252b5132
RH
1499}
1500
1501/* Follows archive_head and produces an extended name table if
1502 necessary. Returns (in tabloc) a pointer to an extended name
1503 table, and in tablen the length of the table. If it makes an entry
1504 it clobbers the filename so that the element may be written without
b34976b6 1505 further massage. Returns TRUE if it ran successfully, FALSE if
252b5132
RH
1506 something went wrong. A successful return may still involve a
1507 zero-length tablen! */
1508
b34976b6 1509bfd_boolean
c58b9523
AM
1510_bfd_construct_extended_name_table (bfd *abfd,
1511 bfd_boolean trailing_slash,
1512 char **tabloc,
1513 bfd_size_type *tablen)
252b5132
RH
1514{
1515 unsigned int maxname = abfd->xvec->ar_max_namelen;
dc810e39 1516 bfd_size_type total_namelen = 0;
252b5132
RH
1517 bfd *current;
1518 char *strptr;
a8da6403
NC
1519 const char *last_filename;
1520 long last_stroff;
252b5132
RH
1521
1522 *tablen = 0;
a8da6403 1523 last_filename = NULL;
252b5132 1524
047066e1 1525 /* Figure out how long the table should be. */
cc481421
AM
1526 for (current = abfd->archive_head;
1527 current != NULL;
1528 current = current->archive_next)
252b5132
RH
1529 {
1530 const char *normal;
1531 unsigned int thislen;
1532
a8da6403
NC
1533 if (bfd_is_thin_archive (abfd))
1534 {
1535 const char *filename = current->filename;
1536
1537 /* If the element being added is a member of another archive
1538 (i.e., we are flattening), use the containing archive's name. */
1539 if (current->my_archive
1540 && ! bfd_is_thin_archive (current->my_archive))
1541 filename = current->my_archive->filename;
1542
1543 /* If the path is the same as the previous path seen,
1544 reuse it. This can happen when flattening a thin
1545 archive that contains other archives. */
007d6189 1546 if (last_filename && filename_cmp (last_filename, filename) == 0)
a8da6403
NC
1547 continue;
1548
1549 last_filename = filename;
1550
1551 /* If the path is relative, adjust it relative to
1552 the containing archive. */
1553 if (! IS_ABSOLUTE_PATH (filename)
1554 && ! IS_ABSOLUTE_PATH (abfd->filename))
1555 normal = adjust_relative_path (filename, abfd->filename);
1556 else
1557 normal = filename;
1558
1559 /* In a thin archive, always store the full pathname
1560 in the extended name table. */
1561 total_namelen += strlen (normal) + 1;
1562 if (trailing_slash)
1563 /* Leave room for trailing slash. */
1564 ++total_namelen;
1565
1566 continue;
1567 }
1568
252b5132
RH
1569 normal = normalize (current, current->filename);
1570 if (normal == NULL)
b34976b6 1571 return FALSE;
252b5132
RH
1572
1573 thislen = strlen (normal);
1574
1575 if (thislen > maxname
1576 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1577 thislen = maxname;
1578
1579 if (thislen > maxname)
1580 {
1581 /* Add one to leave room for \n. */
1582 total_namelen += thislen + 1;
1583 if (trailing_slash)
1584 {
1585 /* Leave room for trailing slash. */
1586 ++total_namelen;
1587 }
1588 }
1589 else
1590 {
1591 struct ar_hdr *hdr = arch_hdr (current);
007d6189 1592 if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
252b5132
RH
1593 || (thislen < sizeof hdr->ar_name
1594 && hdr->ar_name[thislen] != ar_padchar (current)))
1595 {
1596 /* Must have been using extended format even though it
1597 didn't need to. Fix it to use normal format. */
1598 memcpy (hdr->ar_name, normal, thislen);
1599 if (thislen < maxname
1600 || (thislen == maxname && thislen < sizeof hdr->ar_name))
1601 hdr->ar_name[thislen] = ar_padchar (current);
1602 }
1603 }
1604 }
1605
1606 if (total_namelen == 0)
b34976b6 1607 return TRUE;
252b5132 1608
a50b1753 1609 *tabloc = (char *) bfd_zalloc (abfd, total_namelen);
252b5132 1610 if (*tabloc == NULL)
b34976b6 1611 return FALSE;
252b5132
RH
1612
1613 *tablen = total_namelen;
1614 strptr = *tabloc;
1615
a8da6403
NC
1616 last_filename = NULL;
1617 last_stroff = 0;
1618
cc481421
AM
1619 for (current = abfd->archive_head;
1620 current != NULL;
1621 current = current->archive_next)
252b5132
RH
1622 {
1623 const char *normal;
1624 unsigned int thislen;
a8da6403
NC
1625 long stroff;
1626 const char *filename = current->filename;
1627
1628 if (bfd_is_thin_archive (abfd))
1629 {
1630 /* If the element being added is a member of another archive
1631 (i.e., we are flattening), use the containing archive's name. */
1632 if (current->my_archive
1633 && ! bfd_is_thin_archive (current->my_archive))
1634 filename = current->my_archive->filename;
1635 /* If the path is the same as the previous path seen,
1636 reuse it. This can happen when flattening a thin
1637 archive that contains other archives.
1638 If the path is relative, adjust it relative to
1639 the containing archive. */
007d6189 1640 if (last_filename && filename_cmp (last_filename, filename) == 0)
a8da6403
NC
1641 normal = last_filename;
1642 else if (! IS_ABSOLUTE_PATH (filename)
1643 && ! IS_ABSOLUTE_PATH (abfd->filename))
1644 normal = adjust_relative_path (filename, abfd->filename);
1645 else
1646 normal = filename;
1647 }
1648 else
1649 {
1650 normal = normalize (current, filename);
1651 if (normal == NULL)
1652 return FALSE;
1653 }
252b5132
RH
1654
1655 thislen = strlen (normal);
a8da6403 1656 if (thislen > maxname || bfd_is_thin_archive (abfd))
252b5132
RH
1657 {
1658 /* Works for now; may need to be re-engineered if we
1659 encounter an oddball archive format and want to
d70910e8 1660 generalise this hack. */
252b5132 1661 struct ar_hdr *hdr = arch_hdr (current);
a8da6403
NC
1662 if (normal == last_filename)
1663 stroff = last_stroff;
1664 else
1665 {
1666 strcpy (strptr, normal);
1667 if (! trailing_slash)
89a58aeb 1668 strptr[thislen] = ARFMAG[1];
a8da6403
NC
1669 else
1670 {
1671 strptr[thislen] = '/';
89a58aeb 1672 strptr[thislen + 1] = ARFMAG[1];
a8da6403
NC
1673 }
1674 stroff = strptr - *tabloc;
1675 last_stroff = stroff;
252b5132
RH
1676 }
1677 hdr->ar_name[0] = ar_padchar (current);
a8da6403
NC
1678 if (bfd_is_thin_archive (abfd) && current->origin > 0)
1679 {
1680 int len = snprintf (hdr->ar_name + 1, maxname - 1, "%-ld:",
1681 stroff);
1682 _bfd_ar_spacepad (hdr->ar_name + 1 + len, maxname - 1 - len,
1683 "%-ld",
1684 current->origin - sizeof (struct ar_hdr));
1685 }
1686 else
1687 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1688 if (normal != last_filename)
1689 {
1690 strptr += thislen + 1;
1691 if (trailing_slash)
1692 ++strptr;
1693 last_filename = filename;
1694 }
252b5132
RH
1695 }
1696 }
1697
b34976b6 1698 return TRUE;
252b5132 1699}
8f95b6e4
TG
1700
1701/* Do not construct an extended name table but transforms name field into
1702 its extended form. */
1703
1704bfd_boolean
1705_bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
1706 char **tabloc,
1707 bfd_size_type *tablen,
1708 const char **name)
1709{
1710 unsigned int maxname = abfd->xvec->ar_max_namelen;
1711 bfd *current;
1712
1713 *tablen = 0;
1714 *tabloc = NULL;
1715 *name = NULL;
1716
1717 for (current = abfd->archive_head;
1718 current != NULL;
1719 current = current->archive_next)
1720 {
1721 const char *normal = normalize (current, current->filename);
1722 int has_space = 0;
1723 unsigned int len;
1724
1725 if (normal == NULL)
1726 return FALSE;
1727
1728 for (len = 0; normal[len]; len++)
1729 if (normal[len] == ' ')
1730 has_space = 1;
1731
1732 if (len > maxname || has_space)
1733 {
1734 struct ar_hdr *hdr = arch_hdr (current);
1735
1736 len = (len + 3) & ~3;
1737 arch_eltdata (current)->extra_size = len;
21fd2dbd 1738 _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len);
8f95b6e4
TG
1739 }
1740 }
1741
1742 return TRUE;
1743}
1744\f
1745/* Write an archive header. */
1746
1747bfd_boolean
1748_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1749{
1750 struct ar_hdr *hdr = arch_hdr (abfd);
1751
1752 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1753 return FALSE;
1754 return TRUE;
1755}
1756
1757/* Write an archive header using BSD4.4 convention. */
1758
1759bfd_boolean
1760_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1761{
1762 struct ar_hdr *hdr = arch_hdr (abfd);
1763
1764 if (is_bsd44_extended_name (hdr->ar_name))
1765 {
1766 /* This is a BSD 4.4 extended name. */
1767 const char *fullname = normalize (abfd, abfd->filename);
1768 unsigned int len = strlen (fullname);
1769 unsigned int padded_len = (len + 3) & ~3;
1770
1771 BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1772
1773 _bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
1774 arch_eltdata (abfd)->parsed_size + padded_len);
1775
1776 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1777 return FALSE;
1778
1779 if (bfd_bwrite (fullname, len, archive) != len)
1780 return FALSE;
1781 if (len & 3)
1782 {
1783 static const char pad[3] = { 0, 0, 0 };
1784
1785 len = 4 - (len & 3);
1786 if (bfd_bwrite (pad, len, archive) != len)
1787 return FALSE;
1788 }
1789 }
1790 else
1791 {
1792 if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1793 return FALSE;
1794 }
1795 return TRUE;
1796}
252b5132 1797\f
06fc8a8c 1798/* A couple of functions for creating ar_hdrs. */
252b5132 1799
23afc6f6
JL
1800#ifdef HPUX_LARGE_AR_IDS
1801/* Function to encode large UID/GID values according to HP. */
047066e1 1802
23afc6f6 1803static void
c58b9523 1804hpux_uid_gid_encode (char str[6], long int id)
23afc6f6
JL
1805{
1806 int cnt;
1807
1808 str[5] = '@' + (id & 3);
1809 id >>= 2;
1810
174660ce 1811 for (cnt = 4; cnt >= 0; --cnt, id >>= 6)
23afc6f6
JL
1812 str[cnt] = ' ' + (id & 0x3f);
1813}
1814#endif /* HPUX_LARGE_AR_IDS */
1815
633fd09f
ILT
1816#ifndef HAVE_GETUID
1817#define getuid() 0
1818#endif
1819
1820#ifndef HAVE_GETGID
1821#define getgid() 0
1822#endif
1823
252b5132
RH
1824/* Takes a filename, returns an arelt_data for it, or NULL if it can't
1825 make one. The filename must refer to a filename in the filesystem.
1826 The filename field of the ar_hdr will NOT be initialized. If member
d70910e8 1827 is set, and it's an in-memory bfd, we fake it. */
252b5132
RH
1828
1829static struct areltdata *
c58b9523 1830bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
252b5132
RH
1831{
1832 struct stat status;
1833 struct areltdata *ared;
1834 struct ar_hdr *hdr;
dc810e39 1835 bfd_size_type amt;
252b5132
RH
1836
1837 if (member && (member->flags & BFD_IN_MEMORY) != 0)
1838 {
047066e1 1839 /* Assume we just "made" the member, and fake it. */
a50b1753 1840 struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
047066e1
KH
1841 time (&status.st_mtime);
1842 status.st_uid = getuid ();
1843 status.st_gid = getgid ();
252b5132
RH
1844 status.st_mode = 0644;
1845 status.st_size = bim->size;
1846 }
1847 else if (stat (filename, &status) != 0)
1848 {
1849 bfd_set_error (bfd_error_system_call);
1850 return NULL;
1851 }
1852
36e4dce6
CD
1853 /* If the caller requested that the BFD generate deterministic output,
1854 fake values for modification time, UID, GID, and file mode. */
1855 if ((abfd->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
1856 {
1857 status.st_mtime = 0;
1858 status.st_uid = 0;
1859 status.st_gid = 0;
1860 status.st_mode = 0644;
1861 }
1862
dc810e39 1863 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
a50b1753 1864 ared = (struct areltdata *) bfd_zalloc (abfd, amt);
252b5132
RH
1865 if (ared == NULL)
1866 return NULL;
1867 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1868
047066e1 1869 /* ar headers are space padded, not null padded! */
c58b9523 1870 memset (hdr, ' ', sizeof (struct ar_hdr));
252b5132 1871
390c0e42
JJ
1872 _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1873 status.st_mtime);
23afc6f6
JL
1874#ifdef HPUX_LARGE_AR_IDS
1875 /* HP has a very "special" way to handle UID/GID's with numeric values
1876 > 99999. */
1877 if (status.st_uid > 99999)
390c0e42 1878 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
23afc6f6
JL
1879 else
1880#endif
390c0e42
JJ
1881 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1882 status.st_uid);
23afc6f6
JL
1883#ifdef HPUX_LARGE_AR_IDS
1884 /* HP has a very "special" way to handle UID/GID's with numeric values
1885 > 99999. */
1886 if (status.st_gid > 99999)
390c0e42 1887 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
23afc6f6
JL
1888 else
1889#endif
390c0e42
JJ
1890 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1891 status.st_gid);
1892 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1893 status.st_mode);
1894 _bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
1895 status.st_size);
1896 memcpy (hdr->ar_fmag, ARFMAG, 2);
252b5132
RH
1897 ared->parsed_size = status.st_size;
1898 ared->arch_header = (char *) hdr;
1899
1900 return ared;
1901}
1902
047066e1
KH
1903/* Analogous to stat call. */
1904
252b5132 1905int
c58b9523 1906bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
252b5132
RH
1907{
1908 struct ar_hdr *hdr;
1909 char *aloser;
1910
1911 if (abfd->arelt_data == NULL)
1912 {
1913 bfd_set_error (bfd_error_invalid_operation);
1914 return -1;
1915 }
1916
1917 hdr = arch_hdr (abfd);
1918
047066e1
KH
1919#define foo(arelt, stelt, size) \
1920 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1921 if (aloser == hdr->arelt) \
1922 return -1;
1923
23afc6f6
JL
1924 /* Some platforms support special notations for large IDs. */
1925#ifdef HPUX_LARGE_AR_IDS
047066e1
KH
1926# define foo2(arelt, stelt, size) \
1927 if (hdr->arelt[5] == ' ') \
1928 { \
1929 foo (arelt, stelt, size); \
1930 } \
1931 else \
1932 { \
1933 int cnt; \
1934 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1935 { \
1936 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1937 return -1; \
1938 buf->stelt <<= 6; \
1939 buf->stelt += hdr->arelt[cnt] - ' '; \
1940 } \
1941 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1942 return -1; \
1943 buf->stelt <<= 2; \
1944 buf->stelt += hdr->arelt[5] - '@'; \
1945 }
23afc6f6
JL
1946#else
1947# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1948#endif
252b5132
RH
1949
1950 foo (ar_date, st_mtime, 10);
23afc6f6
JL
1951 foo2 (ar_uid, st_uid, 10);
1952 foo2 (ar_gid, st_gid, 10);
252b5132
RH
1953 foo (ar_mode, st_mode, 8);
1954
1955 buf->st_size = arch_eltdata (abfd)->parsed_size;
1956
1957 return 0;
1958}
1959
1960void
c58b9523 1961bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
1962{
1963 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1964 Fortunately ic960 users will never use that option. Fixing this
1965 is very hard; fortunately I know how to do it and will do so once
d70910e8 1966 intel's release is out the door. */
252b5132
RH
1967
1968 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1969 size_t length;
1970 const char *filename;
1971 size_t maxlen = ar_maxnamelen (abfd);
1972
1973 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1974 {
1975 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1976 return;
1977 }
1978
1979 filename = normalize (abfd, pathname);
1980 if (filename == NULL)
1981 {
1982 /* FIXME */
1983 abort ();
1984 }
1985
1986 length = strlen (filename);
1987
1988 if (length <= maxlen)
1989 memcpy (hdr->ar_name, filename, length);
1990
1991 /* Add the padding character if there is room for it. */
1992 if (length < maxlen
1993 || (length == maxlen && length < sizeof hdr->ar_name))
1994 (hdr->ar_name)[length] = ar_padchar (abfd);
1995}
1996
1997void
c58b9523 1998bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
1999{
2000 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39 2001 size_t length;
19172f39 2002 const char *filename = lbasename (pathname);
dc810e39 2003 size_t maxlen = ar_maxnamelen (abfd);
252b5132 2004
252b5132
RH
2005 length = strlen (filename);
2006
2007 if (length <= maxlen)
2008 memcpy (hdr->ar_name, filename, length);
2009 else
2010 {
2011 /* pathname: meet procrustes */
2012 memcpy (hdr->ar_name, filename, maxlen);
2013 length = maxlen;
2014 }
2015
2016 if (length < maxlen)
2017 (hdr->ar_name)[length] = ar_padchar (abfd);
2018}
2019
2020/* Store name into ar header. Truncates the name to fit.
2021 1> strip pathname to be just the basename.
2022 2> if it's short enuf to fit, stuff it in.
2023 3> If it doesn't end with .o, truncate it to fit
2024 4> truncate it before the .o, append .o, stuff THAT in. */
2025
2026/* This is what gnu ar does. It's better but incompatible with the
d70910e8 2027 bsd ar. */
252b5132
RH
2028
2029void
c58b9523 2030bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
2031{
2032 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39 2033 size_t length;
19172f39 2034 const char *filename = lbasename (pathname);
dc810e39 2035 size_t maxlen = ar_maxnamelen (abfd);
252b5132 2036
252b5132
RH
2037 length = strlen (filename);
2038
2039 if (length <= maxlen)
2040 memcpy (hdr->ar_name, filename, length);
2041 else
a8da6403
NC
2042 {
2043 /* pathname: meet procrustes. */
252b5132
RH
2044 memcpy (hdr->ar_name, filename, maxlen);
2045 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
2046 {
2047 hdr->ar_name[maxlen - 2] = '.';
2048 hdr->ar_name[maxlen - 1] = 'o';
2049 }
2050 length = maxlen;
2051 }
2052
2053 if (length < 16)
2054 (hdr->ar_name)[length] = ar_padchar (abfd);
2055}
2056\f
047066e1 2057/* The BFD is open for write and has its format set to bfd_archive. */
252b5132 2058
b34976b6 2059bfd_boolean
c58b9523 2060_bfd_write_archive_contents (bfd *arch)
252b5132
RH
2061{
2062 bfd *current;
2063 char *etable = NULL;
2064 bfd_size_type elength = 0;
2065 const char *ename = NULL;
b34976b6
AM
2066 bfd_boolean makemap = bfd_has_map (arch);
2067 /* If no .o's, don't bother to make a map. */
2068 bfd_boolean hasobjects = FALSE;
252b5132 2069 bfd_size_type wrote;
252b5132 2070 int tries;
a8da6403 2071 char *armag;
252b5132
RH
2072
2073 /* Verify the viability of all entries; if any of them live in the
2074 filesystem (as opposed to living in an archive open for input)
2075 then construct a fresh ar_hdr for them. */
cc481421
AM
2076 for (current = arch->archive_head;
2077 current != NULL;
2078 current = current->archive_next)
252b5132 2079 {
8000a618
DD
2080 /* This check is checking the bfds for the objects we're reading
2081 from (which are usually either an object file or archive on
2082 disk), not the archive entries we're writing to. We don't
2083 actually create bfds for the archive members, we just copy
2084 them byte-wise when we write out the archive. */
252b5132
RH
2085 if (bfd_write_p (current))
2086 {
2087 bfd_set_error (bfd_error_invalid_operation);
ffda70fc 2088 goto input_err;
252b5132
RH
2089 }
2090 if (!current->arelt_data)
2091 {
2092 current->arelt_data =
c58b9523 2093 bfd_ar_hdr_from_filesystem (arch, current->filename, current);
252b5132 2094 if (!current->arelt_data)
ffda70fc 2095 goto input_err;
252b5132 2096
047066e1 2097 /* Put in the file name. */
c58b9523
AM
2098 BFD_SEND (arch, _bfd_truncate_arname,
2099 (arch, current->filename, (char *) arch_hdr (current)));
252b5132
RH
2100 }
2101
2102 if (makemap && ! hasobjects)
047066e1 2103 { /* Don't bother if we won't make a map! */
0e71e495 2104 if ((bfd_check_format (current, bfd_object)))
b34976b6 2105 hasobjects = TRUE;
252b5132
RH
2106 }
2107 }
2108
2109 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2110 (arch, &etable, &elength, &ename)))
b34976b6 2111 return FALSE;
252b5132
RH
2112
2113 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 2114 return FALSE;
a8da6403
NC
2115 armag = ARMAG;
2116 if (bfd_is_thin_archive (arch))
2117 armag = ARMAGT;
2118 wrote = bfd_bwrite (armag, SARMAG, arch);
252b5132 2119 if (wrote != SARMAG)
b34976b6 2120 return FALSE;
252b5132
RH
2121
2122 if (makemap && hasobjects)
2123 {
82e51918 2124 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
b34976b6 2125 return FALSE;
252b5132
RH
2126 }
2127
2128 if (elength != 0)
2129 {
2130 struct ar_hdr hdr;
2131
390c0e42
JJ
2132 memset (&hdr, ' ', sizeof (struct ar_hdr));
2133 memcpy (hdr.ar_name, ename, strlen (ename));
252b5132 2134 /* Round size up to even number in archive header. */
390c0e42
JJ
2135 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
2136 (elength + 1) & ~(bfd_size_type) 1);
2137 memcpy (hdr.ar_fmag, ARFMAG, 2);
c58b9523 2138 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 2139 != sizeof (struct ar_hdr))
dc810e39 2140 || bfd_bwrite (etable, elength, arch) != elength)
b34976b6 2141 return FALSE;
252b5132
RH
2142 if ((elength % 2) == 1)
2143 {
38d6ea5b 2144 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
b34976b6 2145 return FALSE;
252b5132
RH
2146 }
2147 }
2148
cc481421
AM
2149 for (current = arch->archive_head;
2150 current != NULL;
2151 current = current->archive_next)
252b5132
RH
2152 {
2153 char buffer[DEFAULT_BUFFERSIZE];
2154 unsigned int remaining = arelt_size (current);
252b5132 2155
047066e1 2156 /* Write ar header. */
8f95b6e4
TG
2157 if (!_bfd_write_ar_hdr (arch, current))
2158 return FALSE;
a8da6403
NC
2159 if (bfd_is_thin_archive (arch))
2160 continue;
252b5132 2161 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
ffda70fc 2162 goto input_err;
a8da6403 2163
252b5132
RH
2164 while (remaining)
2165 {
2166 unsigned int amt = DEFAULT_BUFFERSIZE;
a8da6403 2167
252b5132
RH
2168 if (amt > remaining)
2169 amt = remaining;
2170 errno = 0;
c58b9523 2171 if (bfd_bread (buffer, amt, current) != amt)
252b5132
RH
2172 {
2173 if (bfd_get_error () != bfd_error_system_call)
ffda70fc
AM
2174 bfd_set_error (bfd_error_file_truncated);
2175 goto input_err;
252b5132 2176 }
c58b9523 2177 if (bfd_bwrite (buffer, amt, arch) != amt)
b34976b6 2178 return FALSE;
252b5132
RH
2179 remaining -= amt;
2180 }
a8da6403 2181
252b5132
RH
2182 if ((arelt_size (current) % 2) == 1)
2183 {
38d6ea5b 2184 if (bfd_bwrite (&ARFMAG[1], 1, arch) != 1)
b34976b6 2185 return FALSE;
252b5132
RH
2186 }
2187 }
2188
2189 if (makemap && hasobjects)
2190 {
2191 /* Verify the timestamp in the archive file. If it would not be
2192 accepted by the linker, rewrite it until it would be. If
2193 anything odd happens, break out and just return. (The
2194 Berkeley linker checks the timestamp and refuses to read the
2195 table-of-contents if it is >60 seconds less than the file's
2196 modified-time. That painful hack requires this painful hack. */
2197 tries = 1;
2198 do
2199 {
2200 if (bfd_update_armap_timestamp (arch))
2201 break;
2202 (*_bfd_error_handler)
2203 (_("Warning: writing archive was slow: rewriting timestamp\n"));
2204 }
2205 while (++tries < 6);
2206 }
2207
b34976b6 2208 return TRUE;
ffda70fc
AM
2209
2210 input_err:
2211 bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
2212 return FALSE;
252b5132
RH
2213}
2214\f
047066e1 2215/* Note that the namidx for the first symbol is 0. */
252b5132 2216
b34976b6 2217bfd_boolean
c58b9523 2218_bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
252b5132
RH
2219{
2220 char *first_name = NULL;
2221 bfd *current;
2222 file_ptr elt_no = 0;
2223 struct orl *map = NULL;
06fc8a8c 2224 unsigned int orl_max = 1024; /* Fine initial default. */
dc810e39 2225 unsigned int orl_count = 0;
06fc8a8c 2226 int stridx = 0;
252b5132
RH
2227 asymbol **syms = NULL;
2228 long syms_max = 0;
b34976b6 2229 bfd_boolean ret;
dc810e39 2230 bfd_size_type amt;
252b5132 2231
d70910e8 2232 /* Dunno if this is the best place for this info... */
252b5132
RH
2233 if (elength != 0)
2234 elength += sizeof (struct ar_hdr);
2235 elength += elength % 2;
2236
c58b9523 2237 amt = orl_max * sizeof (struct orl);
a50b1753 2238 map = (struct orl *) bfd_malloc (amt);
252b5132
RH
2239 if (map == NULL)
2240 goto error_return;
2241
2242 /* We put the symbol names on the arch objalloc, and then discard
2243 them when done. */
a50b1753 2244 first_name = (char *) bfd_alloc (arch, 1);
252b5132
RH
2245 if (first_name == NULL)
2246 goto error_return;
2247
047066e1 2248 /* Drop all the files called __.SYMDEF, we're going to make our own. */
a8da6403
NC
2249 while (arch->archive_head
2250 && strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
cc481421 2251 arch->archive_head = arch->archive_head->archive_next;
252b5132 2252
047066e1 2253 /* Map over each element. */
252b5132 2254 for (current = arch->archive_head;
c58b9523 2255 current != NULL;
cc481421 2256 current = current->archive_next, elt_no++)
252b5132 2257 {
82e51918
AM
2258 if (bfd_check_format (current, bfd_object)
2259 && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
252b5132
RH
2260 {
2261 long storage;
2262 long symcount;
2263 long src_count;
2264
2265 storage = bfd_get_symtab_upper_bound (current);
2266 if (storage < 0)
2267 goto error_return;
2268
2269 if (storage != 0)
2270 {
2271 if (storage > syms_max)
2272 {
2273 if (syms_max > 0)
2274 free (syms);
2275 syms_max = storage;
a50b1753 2276 syms = (asymbol **) bfd_malloc (syms_max);
252b5132
RH
2277 if (syms == NULL)
2278 goto error_return;
2279 }
2280 symcount = bfd_canonicalize_symtab (current, syms);
2281 if (symcount < 0)
2282 goto error_return;
2283
047066e1
KH
2284 /* Now map over all the symbols, picking out the ones we
2285 want. */
252b5132
RH
2286 for (src_count = 0; src_count < symcount; src_count++)
2287 {
2288 flagword flags = (syms[src_count])->flags;
2289 asection *sec = syms[src_count]->section;
2290
a8da6403
NC
2291 if ((flags & BSF_GLOBAL
2292 || flags & BSF_WEAK
2293 || flags & BSF_INDIRECT
1352b58a 2294 || flags & BSF_GNU_UNIQUE
a8da6403 2295 || bfd_is_com_section (sec))
77fb9c28 2296 && ! bfd_is_und_section (sec))
252b5132 2297 {
dc810e39 2298 bfd_size_type namelen;
77fb9c28
NC
2299 struct orl *new_map;
2300
047066e1 2301 /* This symbol will go into the archive header. */
252b5132
RH
2302 if (orl_count == orl_max)
2303 {
2304 orl_max *= 2;
c58b9523 2305 amt = orl_max * sizeof (struct orl);
a50b1753 2306 new_map = (struct orl *) bfd_realloc (map, amt);
c58b9523 2307 if (new_map == NULL)
252b5132
RH
2308 goto error_return;
2309
2310 map = new_map;
2311 }
2312
2313 namelen = strlen (syms[src_count]->name);
dc810e39 2314 amt = sizeof (char *);
a50b1753 2315 map[orl_count].name = (char **) bfd_alloc (arch, amt);
252b5132
RH
2316 if (map[orl_count].name == NULL)
2317 goto error_return;
a50b1753
NC
2318 *(map[orl_count].name) = (char *) bfd_alloc (arch,
2319 namelen + 1);
252b5132
RH
2320 if (*(map[orl_count].name) == NULL)
2321 goto error_return;
2322 strcpy (*(map[orl_count].name), syms[src_count]->name);
dc810e39
AM
2323 map[orl_count].u.abfd = current;
2324 map[orl_count].namidx = stridx;
252b5132
RH
2325
2326 stridx += namelen + 1;
2327 ++orl_count;
77fb9c28 2328 }
252b5132
RH
2329 }
2330 }
2331
2332 /* Now ask the BFD to free up any cached information, so we
2333 don't fill all of memory with symbol tables. */
2334 if (! bfd_free_cached_info (current))
2335 goto error_return;
2336 }
2337 }
2338
047066e1 2339 /* OK, now we have collected all the data, let's write them out. */
252b5132
RH
2340 ret = BFD_SEND (arch, write_armap,
2341 (arch, elength, map, orl_count, stridx));
2342
2343 if (syms_max > 0)
2344 free (syms);
2345 if (map != NULL)
2346 free (map);
2347 if (first_name != NULL)
2348 bfd_release (arch, first_name);
2349
2350 return ret;
2351
2352 error_return:
2353 if (syms_max > 0)
2354 free (syms);
2355 if (map != NULL)
2356 free (map);
2357 if (first_name != NULL)
2358 bfd_release (arch, first_name);
2359
b34976b6 2360 return FALSE;
252b5132
RH
2361}
2362
b34976b6 2363bfd_boolean
c58b9523
AM
2364bsd_write_armap (bfd *arch,
2365 unsigned int elength,
2366 struct orl *map,
2367 unsigned int orl_count,
2368 int stridx)
252b5132
RH
2369{
2370 int padit = stridx & 1;
2371 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2372 unsigned int stringsize = stridx + padit;
d70910e8 2373 /* Include 8 bytes to store ranlibsize and stringsize in output. */
252b5132
RH
2374 unsigned int mapsize = ranlibsize + stringsize + 8;
2375 file_ptr firstreal;
2376 bfd *current = arch->archive_head;
06fc8a8c 2377 bfd *last_elt = current; /* Last element arch seen. */
252b5132
RH
2378 bfd_byte temp[4];
2379 unsigned int count;
2380 struct ar_hdr hdr;
36e4dce6 2381 long uid, gid;
252b5132
RH
2382
2383 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2384
0e29e6e8
AM
2385 /* If deterministic, we use 0 as the timestamp in the map.
2386 Some linkers may require that the archive filesystem modification
2387 time is less than (or near to) the archive map timestamp. Those
2388 linkers should not be used with deterministic mode. (GNU ld and
2389 Gold do not have this restriction.) */
2390 bfd_ardata (arch)->armap_timestamp = 0;
2391 uid = 0;
2392 gid = 0;
36e4dce6
CD
2393 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2394 {
0e29e6e8
AM
2395 struct stat statbuf;
2396
2397 if (stat (arch->filename, &statbuf) == 0)
2398 bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2399 + ARMAP_TIME_OFFSET);
36e4dce6
CD
2400 uid = getuid();
2401 gid = getgid();
2402 }
36e4dce6 2403
390c0e42
JJ
2404 memset (&hdr, ' ', sizeof (struct ar_hdr));
2405 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
252b5132
RH
2406 bfd_ardata (arch)->armap_datepos = (SARMAG
2407 + offsetof (struct ar_hdr, ar_date[0]));
390c0e42
JJ
2408 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2409 bfd_ardata (arch)->armap_timestamp);
36e4dce6
CD
2410 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2411 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
390c0e42
JJ
2412 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
2413 memcpy (hdr.ar_fmag, ARFMAG, 2);
c58b9523 2414 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 2415 != sizeof (struct ar_hdr))
b34976b6 2416 return FALSE;
dc810e39 2417 H_PUT_32 (arch, ranlibsize, temp);
c58b9523 2418 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
b34976b6 2419 return FALSE;
252b5132
RH
2420
2421 for (count = 0; count < orl_count; count++)
2422 {
2423 bfd_byte buf[BSD_SYMDEF_SIZE];
2424
dc810e39 2425 if (map[count].u.abfd != last_elt)
252b5132
RH
2426 {
2427 do
2428 {
8f95b6e4
TG
2429 struct areltdata *ared = arch_eltdata (current);
2430
2431 firstreal += (ared->parsed_size + ared->extra_size
2432 + sizeof (struct ar_hdr));
252b5132 2433 firstreal += firstreal % 2;
cc481421 2434 current = current->archive_next;
252b5132 2435 }
dc810e39 2436 while (current != map[count].u.abfd);
06fc8a8c 2437 }
252b5132
RH
2438
2439 last_elt = current;
dc810e39
AM
2440 H_PUT_32 (arch, map[count].namidx, buf);
2441 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
c58b9523 2442 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
dc810e39 2443 != BSD_SYMDEF_SIZE)
b34976b6 2444 return FALSE;
252b5132
RH
2445 }
2446
047066e1 2447 /* Now write the strings themselves. */
dc810e39 2448 H_PUT_32 (arch, stringsize, temp);
c58b9523 2449 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
b34976b6 2450 return FALSE;
252b5132
RH
2451 for (count = 0; count < orl_count; count++)
2452 {
2453 size_t len = strlen (*map[count].name) + 1;
2454
c58b9523 2455 if (bfd_bwrite (*map[count].name, len, arch) != len)
b34976b6 2456 return FALSE;
252b5132
RH
2457 }
2458
2459 /* The spec sez this should be a newline. But in order to be
d70910e8 2460 bug-compatible for sun's ar we use a null. */
252b5132
RH
2461 if (padit)
2462 {
c58b9523 2463 if (bfd_bwrite ("", 1, arch) != 1)
b34976b6 2464 return FALSE;
252b5132
RH
2465 }
2466
b34976b6 2467 return TRUE;
252b5132
RH
2468}
2469
2470/* At the end of archive file handling, update the timestamp in the
2471 file, so the linker will accept it.
2472
b34976b6
AM
2473 Return TRUE if the timestamp was OK, or an unusual problem happened.
2474 Return FALSE if we updated the timestamp. */
252b5132 2475
b34976b6 2476bfd_boolean
c58b9523 2477_bfd_archive_bsd_update_armap_timestamp (bfd *arch)
252b5132
RH
2478{
2479 struct stat archstat;
2480 struct ar_hdr hdr;
252b5132 2481
36e4dce6
CD
2482 /* If creating deterministic archives, just leave the timestamp as-is. */
2483 if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) != 0)
2484 return TRUE;
2485
252b5132
RH
2486 /* Flush writes, get last-write timestamp from file, and compare it
2487 to the timestamp IN the file. */
2488 bfd_flush (arch);
2489 if (bfd_stat (arch, &archstat) == -1)
2490 {
5fe39cae 2491 bfd_perror (_("Reading archive file mod timestamp"));
047066e1
KH
2492
2493 /* Can't read mod time for some reason. */
b34976b6 2494 return TRUE;
252b5132 2495 }
ae38509c 2496 if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
047066e1 2497 /* OK by the linker's rules. */
b34976b6 2498 return TRUE;
252b5132
RH
2499
2500 /* Update the timestamp. */
2501 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2502
2503 /* Prepare an ASCII version suitable for writing. */
390c0e42
JJ
2504 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2505 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2506 bfd_ardata (arch)->armap_timestamp);
252b5132
RH
2507
2508 /* Write it into the file. */
2509 bfd_ardata (arch)->armap_datepos = (SARMAG
2510 + offsetof (struct ar_hdr, ar_date[0]));
2511 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
c58b9523 2512 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
252b5132
RH
2513 != sizeof (hdr.ar_date)))
2514 {
5fe39cae 2515 bfd_perror (_("Writing updated armap timestamp"));
047066e1
KH
2516
2517 /* Some error while writing. */
b34976b6 2518 return TRUE;
252b5132
RH
2519 }
2520
047066e1 2521 /* We updated the timestamp successfully. */
b34976b6 2522 return FALSE;
252b5132
RH
2523}
2524\f
2525/* A coff armap looks like :
2526 lARMAG
2527 struct ar_hdr with name = '/'
2528 number of symbols
2529 offset of file for symbol 0
2530 offset of file for symbol 1
2531
2532 offset of file for symbol n-1
2533 symbol name 0
2534 symbol name 1
2535
06fc8a8c 2536 symbol name n-1 */
252b5132 2537
b34976b6 2538bfd_boolean
c58b9523
AM
2539coff_write_armap (bfd *arch,
2540 unsigned int elength,
2541 struct orl *map,
2542 unsigned int symbol_count,
2543 int stridx)
252b5132
RH
2544{
2545 /* The size of the ranlib is the number of exported symbols in the
08da05b0 2546 archive * the number of bytes in an int, + an int for the count. */
252b5132
RH
2547 unsigned int ranlibsize = (symbol_count * 4) + 4;
2548 unsigned int stringsize = stridx;
2549 unsigned int mapsize = stringsize + ranlibsize;
dc810e39 2550 unsigned int archive_member_file_ptr;
252b5132
RH
2551 bfd *current = arch->archive_head;
2552 unsigned int count;
2553 struct ar_hdr hdr;
252b5132
RH
2554 int padit = mapsize & 1;
2555
2556 if (padit)
2557 mapsize++;
2558
047066e1 2559 /* Work out where the first object file will go in the archive. */
252b5132
RH
2560 archive_member_file_ptr = (mapsize
2561 + elength
2562 + sizeof (struct ar_hdr)
2563 + SARMAG);
2564
390c0e42 2565 memset (&hdr, ' ', sizeof (struct ar_hdr));
252b5132 2566 hdr.ar_name[0] = '/';
390c0e42
JJ
2567 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
2568 mapsize);
2569 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
36e4dce6
CD
2570 ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2571 ? time (NULL) : 0));
047066e1 2572 /* This, at least, is what Intel coff sets the values to. */
390c0e42
JJ
2573 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2574 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2575 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2576 memcpy (hdr.ar_fmag, ARFMAG, 2);
252b5132 2577
047066e1 2578 /* Write the ar header for this item and the number of symbols. */
c58b9523 2579 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 2580 != sizeof (struct ar_hdr))
b34976b6 2581 return FALSE;
252b5132 2582
4dae1ae7 2583 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
b34976b6 2584 return FALSE;
252b5132
RH
2585
2586 /* Two passes, first write the file offsets for each symbol -
2587 remembering that each offset is on a two byte boundary. */
2588
2589 /* Write out the file offset for the file associated with each
2590 symbol, and remember to keep the offsets padded out. */
2591
2592 current = arch->archive_head;
2593 count = 0;
c58b9523 2594 while (current != NULL && count < symbol_count)
252b5132 2595 {
047066e1
KH
2596 /* For each symbol which is used defined in this object, write
2597 out the object file's address in the archive. */
252b5132 2598
dc810e39 2599 while (count < symbol_count && map[count].u.abfd == current)
252b5132 2600 {
4dae1ae7 2601 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
b34976b6 2602 return FALSE;
252b5132
RH
2603 count++;
2604 }
a8da6403
NC
2605 archive_member_file_ptr += sizeof (struct ar_hdr);
2606 if (! bfd_is_thin_archive (arch))
2607 {
2608 /* Add size of this archive entry. */
2609 archive_member_file_ptr += arelt_size (current);
2610 /* Remember about the even alignment. */
2611 archive_member_file_ptr += archive_member_file_ptr % 2;
2612 }
cc481421 2613 current = current->archive_next;
252b5132
RH
2614 }
2615
047066e1 2616 /* Now write the strings themselves. */
252b5132
RH
2617 for (count = 0; count < symbol_count; count++)
2618 {
2619 size_t len = strlen (*map[count].name) + 1;
2620
c58b9523 2621 if (bfd_bwrite (*map[count].name, len, arch) != len)
b34976b6 2622 return FALSE;
252b5132
RH
2623 }
2624
2625 /* The spec sez this should be a newline. But in order to be
d70910e8 2626 bug-compatible for arc960 we use a null. */
252b5132
RH
2627 if (padit)
2628 {
c58b9523 2629 if (bfd_bwrite ("", 1, arch) != 1)
b34976b6 2630 return FALSE;
252b5132
RH
2631 }
2632
b34976b6 2633 return TRUE;
252b5132 2634}
This page took 0.642476 seconds and 4 git commands to generate.