Coverage Report

Created: 2026-11-21 09:00

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