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