Coverage Report

Created: 2026-09-14 08:07

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.34k
#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
217
27.0M
#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
218
219
20.6M
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
220
2.95k
#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
14.8M
  (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
3.52k
{
230
3.52k
  char buf[20];
231
3.52k
  size_t len;
232
233
3.52k
  snprintf (buf, sizeof (buf), fmt, val);
234
3.52k
  len = strlen (buf);
235
3.52k
  if (len < n)
236
2.27k
    {
237
2.27k
      memcpy (p, buf, len);
238
2.27k
      memset (p + len, ' ', n - len);
239
2.27k
    }
240
1.25k
  else
241
1.25k
    memcpy (p, buf, n);
242
3.52k
}
243
244
bool
245
_bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
246
844
{
247
844
  char buf[21];
248
844
  size_t len;
249
250
844
  snprintf (buf, sizeof (buf), "%-10" PRIu64, (uint64_t) size);
251
844
  len = strlen (buf);
252
844
  if (len > n)
253
0
    {
254
0
      bfd_set_error (bfd_error_file_too_big);
255
0
      return false;
256
0
    }
257
844
  if (len < n)
258
0
    {
259
0
      memcpy (p, buf, len);
260
0
      memset (p + len, ' ', n - len);
261
0
    }
262
844
  else
263
844
    memcpy (p, buf, n);
264
844
  return true;
265
844
}
266

267
bool
268
_bfd_generic_mkarchive (bfd *abfd)
269
3.65k
{
270
3.65k
  size_t amt = sizeof (struct artdata);
271
272
3.65k
  abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
273
3.65k
  return bfd_ardata (abfd) != NULL;
274
3.65k
}
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
10.1M
{
323
10.1M
  return _bfd_new_bfd_contained_in (obfd);
324
10.1M
}
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
10.2M
{
348
10.2M
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
349
10.2M
  struct ar_cache m;
350
351
10.2M
  m.ptr = filepos;
352
353
10.2M
  if (hash_table)
354
13.7k
    {
355
13.7k
      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
356
13.7k
      if (!entry)
357
13.7k
  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
13.7k
    }
365
10.1M
  else
366
10.1M
    return NULL;
367
10.2M
}
368
369
static hashval_t
370
hash_file_ptr (const void * p)
371
47.9k
{
372
47.9k
  return (hashval_t) (((struct ar_cache *) p)->ptr);
373
47.9k
}
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
17.3k
{
380
17.3k
  struct ar_cache *arc1 = (struct ar_cache *) p1;
381
17.3k
  struct ar_cache *arc2 = (struct ar_cache *) p2;
382
17.3k
  return arc1->ptr == arc2->ptr;
383
17.3k
}
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
16.4k
{
391
16.4k
  return calloc (a, b);
392
16.4k
}
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
17.0k
{
399
17.0k
  struct ar_cache *cache;
400
17.0k
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
401
402
  /* If the hash table hasn't been created, create it.  */
403
17.0k
  if (hash_table == NULL)
404
8.12k
    {
405
8.12k
      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
406
8.12k
              NULL, _bfd_calloc_wrapper, free);
407
8.12k
      if (hash_table == NULL)
408
0
  return false;
409
8.12k
      bfd_ardata (arch_bfd)->cache = hash_table;
410
8.12k
    }
411
412
  /* Insert new_elt into the hash table by filepos.  */
413
17.0k
  cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
414
17.0k
  cache->ptr = filepos;
415
17.0k
  cache->arbfd = new_elt;
416
17.0k
  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
417
418
  /* Provide a means of accessing this from child.  */
419
17.0k
  arch_eltdata (new_elt)->parent_cache = hash_table;
420
17.0k
  arch_eltdata (new_elt)->key = filepos;
421
422
17.0k
  return true;
423
17.0k
}
424

425
static bfd *
426
open_nested_file (const char *filename, bfd *archive)
427
18.8k
{
428
18.8k
  const char *target;
429
18.8k
  bfd *n_bfd;
430
431
18.8k
  target = NULL;
432
18.8k
  if (!archive->target_defaulted)
433
0
    target = archive->xvec->name;
434
18.8k
  n_bfd = bfd_openr (filename, target);
435
18.8k
  if (n_bfd != NULL)
436
16.2k
    {
437
16.2k
      n_bfd->lto_output = archive->lto_output;
438
16.2k
      n_bfd->no_export = archive->no_export;
439
16.2k
      n_bfd->my_archive = archive;
440
16.2k
    }
441
18.8k
  return n_bfd;
442
18.8k
}
443
444
static bfd *
445
find_nested_archive (const char *filename, bfd *arch_bfd)
446
907
{
447
907
  bfd *abfd;
448
449
  /* PR 15140: Don't allow a nested archive pointing to itself.  */
450
907
  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
907
  for (abfd = arch_bfd->nested_archives;
457
907
       abfd != NULL;
458
907
       abfd = abfd->archive_next)
459
486
    {
460
486
      if (filename_cmp (filename, bfd_get_filename (abfd)) == 0)
461
486
  return abfd;
462
486
    }
463
421
  abfd = open_nested_file (filename, arch_bfd);
464
421
  if (abfd)
465
19
    {
466
19
      abfd->archive_next = arch_bfd->nested_archives;
467
19
      arch_bfd->nested_archives = abfd;
468
19
    }
469
421
  return abfd;
470
907
}
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
14.0k
{
478
14.0k
  unsigned long table_index = 0;
479
14.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
14.0k
  errno = 0;
484
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
485
14.0k
  table_index = strtol (name + 1, (char **) &endp, 10);
486
14.0k
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
487
3.50k
    {
488
3.50k
      bfd_set_error (bfd_error_malformed_archive);
489
3.50k
      return NULL;
490
3.50k
    }
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
10.5k
  if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
494
2.69k
    {
495
2.69k
      file_ptr origin = strtol (endp + 1, NULL, 10);
496
497
2.69k
      if (errno != 0)
498
103
  {
499
103
    bfd_set_error (bfd_error_malformed_archive);
500
103
    return NULL;
501
103
  }
502
2.59k
      *originp = origin;
503
2.59k
    }
504
7.87k
  else
505
7.87k
    *originp = 0;
506
507
10.4k
  return bfd_ardata (arch)->extended_names + table_index;
508
10.5k
}
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
15.1M
{
521
15.1M
  return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
522
15.1M
}
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
15.2M
{
530
15.2M
  struct ar_hdr hdr;
531
15.2M
  char *hdrp = (char *) &hdr;
532
15.2M
  uint64_t parsed_size;
533
15.2M
  struct areltdata *ared;
534
15.2M
  char *filename = NULL;
535
15.2M
  ufile_ptr filesize;
536
15.2M
  bfd_size_type namelen = 0;
537
15.2M
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
538
15.2M
  char *allocptr = 0;
539
15.2M
  file_ptr origin = 0;
540
15.2M
  unsigned int extra_size = 0;
541
15.2M
  char fmag_save;
542
15.2M
  int scan;
543
544
15.2M
  if (bfd_read (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
545
25.1k
    {
546
25.1k
      if (bfd_get_error () != bfd_error_system_call)
547
25.1k
  bfd_set_error (bfd_error_no_more_archived_files);
548
25.1k
      return NULL;
549
25.1k
    }
550
15.1M
  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
551
200k
      && (mag == NULL
552
1.23k
    || strncmp (hdr.ar_fmag, mag, 2) != 0))
553
199k
    {
554
199k
      bfd_set_error (bfd_error_malformed_archive);
555
199k
      return NULL;
556
199k
    }
557
558
15.1M
  errno = 0;
559
14.9M
  fmag_save = hdr.ar_fmag[0];
560
14.9M
  hdr.ar_fmag[0] = 0;
561
14.9M
  scan = sscanf (hdr.ar_size, "%" SCNu64, &parsed_size);
562
14.9M
  hdr.ar_fmag[0] = fmag_save;
563
14.9M
  if (scan != 1)
564
135k
    {
565
135k
      bfd_set_error (bfd_error_malformed_archive);
566
135k
      return NULL;
567
135k
    }
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
14.8M
  if ((hdr.ar_name[0] == '/'
573
10.5M
       || (hdr.ar_name[0] == ' '
574
174k
     && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
575
4.48M
      && bfd_ardata (abfd)->extended_names != NULL)
576
14.0k
    {
577
14.0k
      filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
578
14.0k
      if (filename == NULL)
579
3.60k
  return NULL;
580
14.0k
    }
581
  /* BSD4.4-style long filename.  */
582
14.8M
  else if (is_bsd44_extended_name (hdr.ar_name))
583
33.2k
    {
584
      /* BSD-4.4 extended name */
585
33.2k
      namelen = atoi (&hdr.ar_name[3]);
586
33.2k
      filesize = bfd_get_file_size (abfd);
587
33.2k
      if (namelen > parsed_size
588
32.2k
    || namelen > -allocsize - 2
589
32.2k
    || (filesize != 0 && namelen > filesize))
590
1.49k
  {
591
1.49k
    bfd_set_error (bfd_error_malformed_archive);
592
1.49k
    return NULL;
593
1.49k
  }
594
31.7k
      allocsize += namelen + 1;
595
31.7k
      parsed_size -= namelen;
596
31.7k
      extra_size = namelen;
597
598
31.7k
      allocptr = (char *) bfd_malloc (allocsize);
599
31.7k
      if (allocptr == NULL)
600
0
  return NULL;
601
31.7k
      filename = (allocptr
602
31.7k
      + sizeof (struct areltdata)
603
31.7k
      + sizeof (struct ar_hdr));
604
31.7k
      if (bfd_read (filename, namelen, abfd) != namelen)
605
385
  {
606
385
    free (allocptr);
607
385
    if (bfd_get_error () != bfd_error_system_call)
608
385
      bfd_set_error (bfd_error_no_more_archived_files);
609
385
    return NULL;
610
385
  }
611
31.3k
      filename[namelen] = '\0';
612
31.3k
    }
613
14.8M
  else
614
14.8M
    {
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
14.8M
      char *e;
620
14.8M
      e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
621
14.8M
      if (e == NULL)
622
8.19M
  {
623
8.19M
    e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
624
8.19M
    if (e == NULL)
625
3.03M
      e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
626
8.19M
  }
627
628
14.8M
      if (e != NULL)
629
13.9M
  namelen = e - hdr.ar_name;
630
865k
      else
631
865k
  {
632
    /* If we didn't find a termination character, then the name
633
       must be the entire field.  */
634
865k
    namelen = ar_maxnamelen (abfd);
635
865k
  }
636
637
14.8M
      allocsize += namelen + 1;
638
14.8M
    }
639
640
14.8M
  if (!allocptr)
641
14.8M
    {
642
14.8M
      allocptr = (char *) bfd_malloc (allocsize);
643
14.8M
      if (allocptr == NULL)
644
0
  return NULL;
645
14.8M
    }
646
647
14.8M
  memset (allocptr, 0, sizeof (struct areltdata));
648
14.8M
  ared = (struct areltdata *) allocptr;
649
14.8M
  ared->arch_header = allocptr + sizeof (struct areltdata);
650
14.8M
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
651
14.8M
  ared->parsed_size = parsed_size;
652
14.8M
  ared->extra_size = extra_size;
653
14.8M
  ared->origin = origin;
654
655
14.8M
  if (filename != NULL)
656
41.8k
    ared->filename = filename;
657
14.8M
  else
658
14.8M
    {
659
14.8M
      ared->filename = allocptr + (sizeof (struct areltdata) +
660
14.8M
           sizeof (struct ar_hdr));
661
14.8M
      if (namelen)
662
10.4M
  memcpy (ared->filename, hdr.ar_name, namelen);
663
14.8M
      ared->filename[namelen] = '\0';
664
14.8M
    }
665
666
14.8M
  return ared;
667
14.8M
}
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
17.9k
{
675
17.9k
  const char *arch_name = bfd_get_filename (arch);
676
17.9k
  const char *base_name = arch_name != NULL ? lbasename (arch_name) : NULL;
677
17.9k
  size_t prefix_len;
678
17.9k
  char *filename;
679
680
17.9k
  if (base_name == arch_name)
681
0
    return elt_name;
682
683
17.9k
  prefix_len = base_name - arch_name;
684
17.9k
  filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
685
17.9k
  if (filename == NULL)
686
0
    return NULL;
687
688
17.9k
  strncpy (filename, arch_name, prefix_len);
689
17.9k
  strcpy (filename + prefix_len, elt_name);
690
17.9k
  return filename;
691
17.9k
}
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
10.2M
{
701
10.2M
  struct areltdata *new_areldata;
702
10.2M
  bfd *n_bfd;
703
10.2M
  char *filename;
704
705
10.2M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
706
707
10.2M
  n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos);
708
10.2M
  if (n_bfd)
709
0
    return n_bfd;
710
711
10.2M
  if (0 > bfd_seek (archive, filepos, SEEK_SET))
712
198
    return NULL;
713
714
10.2M
  if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
715
17.6k
    return NULL;
716
717
10.1M
  filename = new_areldata->filename;
718
719
10.1M
  if (bfd_is_thin_archive (archive))
720
19.2k
    {
721
      /* This is a proxy entry for an external file.  */
722
19.2k
      if (! IS_ABSOLUTE_PATH (filename))
723
17.9k
  {
724
17.9k
    filename = _bfd_append_relative_path (archive, filename);
725
17.9k
    if (filename == NULL)
726
0
      {
727
0
        free (new_areldata);
728
0
        return NULL;
729
0
      }
730
17.9k
  }
731
732
19.2k
      if (new_areldata->origin > 0)
733
907
  {
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
907
    bfd *ext_arch = find_nested_archive (filename, archive);
737
907
    file_ptr origin = new_areldata->origin;
738
739
907
    free (new_areldata);
740
907
    if (ext_arch == NULL
741
505
        || ! bfd_check_format (ext_arch, bfd_archive))
742
907
      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
18.3k
      bfd_set_error (bfd_error_no_error);
760
18.3k
      n_bfd = open_nested_file (filename, archive);
761
18.3k
      if (n_bfd == NULL)
762
2.16k
  {
763
2.16k
    switch (bfd_get_error ())
764
2.16k
      {
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.16k
      case bfd_error_system_call:
771
2.16k
        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.16k
        break;
779
2.16k
      }
780
2.16k
  }
781
18.3k
    }
782
10.1M
  else
783
10.1M
    {
784
10.1M
      n_bfd = _bfd_create_empty_archive_element_shell (archive);
785
10.1M
    }
786
787
10.1M
  if (n_bfd == NULL)
788
2.16k
    {
789
2.16k
      free (new_areldata);
790
2.16k
      return NULL;
791
2.16k
    }
792
793
10.1M
  n_bfd->proxy_handle.file_offset = bfd_tell (archive);
794
795
10.1M
  if (bfd_is_thin_archive (archive))
796
16.2k
    {
797
16.2k
      n_bfd->origin = 0;
798
16.2k
    }
799
10.1M
  else
800
10.1M
    {
801
10.1M
      n_bfd->origin = n_bfd->proxy_handle.file_offset;
802
10.1M
      if (!bfd_set_filename (n_bfd, filename))
803
0
  goto out;
804
10.1M
    }
805
806
10.1M
  n_bfd->arelt_data = new_areldata;
807
808
  /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags.  */
809
10.1M
  n_bfd->flags |= archive->flags & (BFD_COMPRESS
810
10.1M
            | BFD_DECOMPRESS
811
10.1M
            | BFD_COMPRESS_GABI);
812
813
  /* Copy is_linker_input.  */
814
10.1M
  n_bfd->is_linker_input = archive->is_linker_input;
815
816
10.1M
  if (archive->no_element_cache
817
17.0k
      || _bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd))
818
10.1M
    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
10.1M
}
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
10.2M
{
866
10.2M
  if ((bfd_get_format (archive) != bfd_archive)
867
10.2M
      || (archive->direction == write_direction))
868
0
    {
869
0
      bfd_set_error (bfd_error_invalid_operation);
870
0
      return NULL;
871
0
    }
872
873
10.2M
  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
10.2M
  else
877
10.2M
    return BFD_SEND (archive,
878
10.2M
         openr_next_archived_file, (archive, last_file));
879
10.2M
}
880
881
bfd *
882
bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
883
10.1M
{
884
10.1M
  ufile_ptr filestart;
885
886
10.1M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
887
888
10.1M
  if (!last_file)
889
10.1M
    filestart = bfd_ardata (archive)->first_file.file_offset;
890
12.1k
  else
891
12.1k
    {
892
12.1k
      filestart = last_file->proxy_handle.file_offset;
893
12.1k
      if (! bfd_is_thin_archive (archive))
894
11.9k
  {
895
11.9k
    bfd_size_type size = arelt_size (last_file);
896
897
11.9k
    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
11.9k
    filestart += filestart % 2;
902
11.9k
    if (filestart < last_file->proxy_handle.file_offset)
903
290
      {
904
        /* Prevent looping.  See PR19256.  */
905
290
        bfd_set_error (bfd_error_malformed_archive);
906
290
        return NULL;
907
290
      }
908
11.9k
  }
909
12.1k
    }
910
911
10.1M
  return _bfd_get_elt_at_filepos (archive, filestart, NULL);
912
10.1M
}
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
34.1M
{
924
34.1M
  char armag[SARMAG + 1];
925
34.1M
  size_t amt;
926
927
34.1M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
928
929
34.1M
  if (bfd_read (armag, SARMAG, abfd) != SARMAG)
930
189k
    {
931
189k
      if (bfd_get_error () != bfd_error_system_call)
932
70.7k
  bfd_set_error (bfd_error_wrong_format);
933
189k
      return NULL;
934
189k
    }
935
936
33.9M
  bfd_set_thin_archive (abfd, strncmp (armag, ARMAGT, SARMAG) == 0);
937
938
33.9M
  if (strncmp (armag, ARMAG, SARMAG) != 0
939
22.5M
      && ! bfd_is_thin_archive (abfd))
940
22.4M
    {
941
22.4M
      bfd_set_error (bfd_error_wrong_format);
942
22.4M
      return NULL;
943
22.4M
    }
944
945
11.4M
  amt = sizeof (struct artdata);
946
11.4M
  bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
947
11.4M
  if (bfd_ardata (abfd) == NULL)
948
0
    return NULL;
949
950
11.4M
  bfd_ardata (abfd)->first_file.file_offset = SARMAG;
951
952
11.4M
  if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
953
10.2M
      || !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
10.1M
  if (abfd->target_defaulted || abfd->is_linker_input)
962
10.1M
    {
963
10.1M
      bfd *first;
964
10.1M
      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
10.1M
      save = abfd->no_element_cache;
977
10.1M
      abfd->no_element_cache = 1;
978
10.1M
      first = bfd_openr_next_archived_file (abfd, NULL);
979
10.1M
      abfd->no_element_cache = save;
980
10.1M
      if (first != NULL)
981
10.1M
  {
982
10.1M
    first->target_defaulted = false;
983
10.1M
    if (abfd->is_linker_input)
984
0
      first->plugin_format = bfd_plugin_no;
985
10.1M
    if (!bfd_check_format (first, bfd_object)
986
56.9k
        || first->xvec != abfd->xvec)
987
10.1M
      bfd_set_error (bfd_error_wrong_object_format);
988
10.1M
    bfd_close (first);
989
10.1M
  }
990
10.1M
    }
991
992
10.1M
  return _bfd_no_cleanup;
993
11.4M
}
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.55M
#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
5.40M
#define BSD_SYMDEF_COUNT_SIZE 4
1076
1077
/* The size of the string count.  */
1078
5.40M
#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
2.08M
{
1086
2.08M
  struct areltdata *mapdata;
1087
2.08M
  size_t counter;
1088
2.08M
  bfd_byte *raw_armap, *rbase;
1089
2.08M
  struct artdata *ardata = bfd_ardata (abfd);
1090
2.08M
  char *stringbase;
1091
2.08M
  bfd_size_type parsed_size;
1092
2.08M
  size_t amt, string_size;
1093
2.08M
  carsym *set;
1094
1095
2.08M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1096
1097
2.08M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1098
2.08M
  if (mapdata == NULL)
1099
79.1k
    return false;
1100
2.00M
  parsed_size = mapdata->parsed_size;
1101
2.00M
  free (mapdata);
1102
  /* PR 17512: file: 883ff754.  */
1103
  /* PR 17512: file: 0458885f.  */
1104
2.00M
  if (parsed_size < BSD_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
1105
1.84k
    {
1106
1.84k
      bfd_set_error (bfd_error_malformed_archive);
1107
1.84k
      return false;
1108
1.84k
    }
1109
1110
2.00M
  raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, parsed_size, parsed_size);
1111
2.00M
  if (raw_armap == NULL)
1112
92.2k
    return false;
1113
1114
1.91M
  parsed_size -= BSD_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE;
1115
1.91M
  amt = H_GET_32 (abfd, raw_armap);
1116
1.91M
  if (amt > parsed_size
1117
1.49M
      || amt % BSD_SYMDEF_SIZE != 0)
1118
419k
    {
1119
      /* Probably we're using the wrong byte ordering.  */
1120
419k
      bfd_set_error (bfd_error_wrong_format);
1121
419k
      goto release_armap;
1122
419k
    }
1123
1124
1.49M
  rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
1125
1.49M
  stringbase = (char *) rbase + amt + BSD_STRING_COUNT_SIZE;
1126
1.49M
  string_size = parsed_size - amt;
1127
1128
1.49M
  ardata->symdef_count = amt / BSD_SYMDEF_SIZE;
1129
1.49M
  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.49M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1135
1.49M
  if (!ardata->symdefs)
1136
0
    goto release_armap;
1137
1138
1.49M
  for (counter = 0, set = ardata->symdefs;
1139
2.05M
       counter < ardata->symdef_count;
1140
1.49M
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
1141
582k
    {
1142
582k
      unsigned nameoff = H_GET_32 (abfd, rbase);
1143
582k
      if (nameoff >= string_size)
1144
13.9k
  {
1145
13.9k
    bfd_set_error (bfd_error_malformed_archive);
1146
13.9k
    goto release_armap;
1147
13.9k
  }
1148
568k
      set->name = stringbase + nameoff;
1149
568k
      set->u.file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1150
568k
    }
1151
1152
1.47M
  ardata->first_file.file_offset = bfd_tell (abfd);
1153
  /* Pad to an even boundary if you have to.  */
1154
1.47M
  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.47M
  abfd->has_armap = true;
1159
1.47M
  return true;
1160
1161
433k
 release_armap:
1162
433k
  ardata->symdef_count = 0;
1163
433k
  ardata->symdefs = NULL;
1164
433k
  bfd_release (abfd, raw_armap);
1165
433k
  return false;
1166
1.49M
}
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.37M
{
1174
1.37M
  struct areltdata *mapdata;
1175
1.37M
  int *raw_armap, *rawptr;
1176
1.37M
  struct artdata *ardata = bfd_ardata (abfd);
1177
1.37M
  char *stringbase;
1178
1.37M
  char *stringend;
1179
1.37M
  bfd_size_type stringsize;
1180
1.37M
  bfd_size_type parsed_size;
1181
1.37M
  ufile_ptr filesize;
1182
1.37M
  size_t nsymz, carsym_size, ptrsize, i;
1183
1.37M
  carsym *carsyms;
1184
1.37M
  bfd_vma (*swap) (const void *);
1185
1.37M
  char int_buf[4];
1186
1.37M
  struct areltdata *tmp;
1187
1188
1.37M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1189
1190
1.37M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1191
1.37M
  if (mapdata == NULL)
1192
194k
    return false;
1193
1.18M
  parsed_size = mapdata->parsed_size;
1194
1.18M
  free (mapdata);
1195
1196
1.18M
  if (bfd_read (int_buf, 4, abfd) != 4)
1197
1.63k
    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.18M
  swap = bfd_getb32;
1202
1.18M
  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.18M
  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.18M
  filesize = bfd_get_file_size (abfd);
1214
1.18M
  ptrsize = 4 * nsymz;
1215
1.18M
  if ((filesize != 0 && parsed_size > filesize)
1216
1.16M
      || parsed_size < 4
1217
1.15M
      || parsed_size - 4 < ptrsize)
1218
38.9k
    {
1219
38.9k
      bfd_set_error (bfd_error_malformed_archive);
1220
38.9k
      return false;
1221
38.9k
    }
1222
1223
1.14M
  stringsize = parsed_size - ptrsize - 4;
1224
1225
1.14M
  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
1.14M
  raw_armap = (int *) _bfd_malloc_and_read (abfd, ptrsize, ptrsize);
1233
1.14M
  if (raw_armap == NULL)
1234
2.81k
    return false;
1235
1236
1.14M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd,
1237
1.14M
             carsym_size + stringsize + 1);
1238
1.14M
  if (ardata->symdefs == NULL)
1239
0
    goto free_armap;
1240
1.14M
  carsyms = ardata->symdefs;
1241
1.14M
  stringbase = ((char *) ardata->symdefs) + carsym_size;
1242
1243
1.14M
  if (bfd_read (stringbase, stringsize, abfd) != stringsize)
1244
2.57k
    goto release_symdefs;
1245
1246
  /* OK, build the carsyms.  */
1247
1.13M
  stringend = stringbase + stringsize;
1248
1.13M
  *stringend = 0;
1249
4.57M
  for (i = 0; i < nsymz; i++)
1250
3.43M
    {
1251
3.43M
      rawptr = raw_armap + i;
1252
3.43M
      carsyms->u.file_offset = swap ((bfd_byte *) rawptr);
1253
3.43M
      carsyms->name = stringbase;
1254
3.43M
      stringbase += strlen (stringbase);
1255
3.43M
      if (stringbase != stringend)
1256
2.88M
  ++stringbase;
1257
3.43M
      carsyms++;
1258
3.43M
    }
1259
1260
1.13M
  ardata->symdef_count = nsymz;
1261
1.13M
  ardata->first_file.file_offset = bfd_tell (abfd);
1262
  /* Pad to an even boundary if you have to.  */
1263
1.13M
  ardata->first_file.file_offset += (ardata->first_file.file_offset) % 2;
1264
1.13M
  if (bfd_seek (abfd, ardata->first_file.file_offset, SEEK_SET) != 0)
1265
0
    goto release_symdefs;
1266
1267
1.13M
  abfd->has_armap = true;
1268
1.13M
  free (raw_armap);
1269
1270
  /* Check for a second archive header (as used by PE).  */
1271
1.13M
  tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1272
1.13M
  if (tmp != NULL)
1273
1.13M
    {
1274
1.13M
      if (tmp->arch_header[0] == '/'
1275
122k
    && tmp->arch_header[1] == ' ')
1276
6.62k
  ardata->first_file.file_offset
1277
6.62k
    += (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1278
1.13M
      free (tmp);
1279
1.13M
    }
1280
1281
1.13M
  return true;
1282
1283
2.57k
 release_symdefs:
1284
2.57k
  bfd_release (abfd, (ardata)->symdefs);
1285
2.57k
 free_armap:
1286
2.57k
  free (raw_armap);
1287
2.57k
  return false;
1288
2.57k
}
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
11.2M
{
1296
11.2M
  char nextname[16];
1297
11.2M
  int i = bfd_read (nextname, 16, abfd);
1298
1299
11.2M
  if (i == 0)
1300
6
    return true;
1301
11.2M
  if (i != 16)
1302
5.06k
    return false;
1303
1304
11.2M
  if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1305
0
    return false;
1306
1307
11.2M
  if (memcmp (nextname, "__.SYMDEF       ", 16) == 0
1308
      /* Old Linux archives.  */
1309
10.0M
      || memcmp (nextname, "__.SYMDEF/      ", 16) == 0)
1310
2.08M
    return do_slurp_bsd_armap (abfd);
1311
9.12M
  else if (memcmp (nextname, "/               ", 16) == 0)
1312
1.37M
    return do_slurp_coff_armap (abfd);
1313
7.74M
  else if (memcmp (nextname, "/SYM64/         ", 16) == 0)
1314
134k
    {
1315
      /* 64bit (Irix 6) archive.  */
1316
134k
#ifdef BFD64
1317
134k
      return _bfd_archive_64_bit_slurp_armap (abfd);
1318
#else
1319
      bfd_set_error (bfd_error_wrong_format);
1320
      return false;
1321
#endif
1322
134k
    }
1323
7.60M
  else if (memcmp (nextname, "________", 8) == 0
1324
372k
     && ((nextname[8] == '_' && nextname[9] == '_')
1325
219k
         || (nextname[8] == '6' && nextname[9] == '4'))
1326
277k
     && nextname[10] == 'E'
1327
208k
     && (nextname[11] == 'B' || nextname[11] == 'L')
1328
199k
     && nextname[12] == 'E'
1329
189k
     && (nextname[13] == 'B' || nextname[13] == 'L')
1330
182k
     && nextname[14] == '_'
1331
177k
     && (nextname[15] == ' ' || nextname[15] == 'X'))
1332
171k
    {
1333
      /* ECOFF archive.  */
1334
171k
      bfd_set_error (bfd_error_wrong_format);
1335
171k
      return false;
1336
171k
    }
1337
7.43M
  else if (memcmp (nextname, "#1/20           ", 16) == 0)
1338
21.9k
    {
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
21.9k
      struct ar_hdr hdr;
1343
21.9k
      char extname[20];
1344
1345
21.9k
      if (bfd_read (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1346
4.60k
  return false;
1347
      /* Read the extended name.  We know its length.  */
1348
17.3k
      if (bfd_read (extname, 20, abfd) != 20)
1349
6.67k
  return false;
1350
10.6k
      if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1351
0
  return false;
1352
10.6k
      if (memcmp (extname, "__.SYMDEF SORTED", 16) == 0
1353
9.30k
    || memcmp (extname, "__.SYMDEF", 9) == 0)
1354
2.99k
  return do_slurp_bsd_armap (abfd);
1355
10.6k
    }
1356
1357
7.42M
  abfd->has_armap = false;
1358
7.42M
  return true;
1359
11.2M
}
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
10.2M
{
1376
10.2M
  char nextname[17];
1377
1378
10.2M
  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
10.2M
  if (bfd_seek (abfd, bfd_ardata (abfd)->first_file.file_offset,
1383
10.2M
    SEEK_SET) != 0)
1384
0
    return false;
1385
1386
10.2M
  if (bfd_read (nextname, 16, abfd) == 16)
1387
10.2M
    {
1388
10.2M
      struct areltdata *namedata;
1389
10.2M
      bfd_size_type amt;
1390
10.2M
      ufile_ptr filesize;
1391
1392
10.2M
      if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1393
0
  return false;
1394
1395
10.2M
      if (! startswith (nextname, "ARFILENAMES/    ")
1396
10.1M
    && ! startswith (nextname, "//              "))
1397
9.99M
  {
1398
9.99M
    bfd_ardata (abfd)->extended_names = NULL;
1399
9.99M
    bfd_ardata (abfd)->extended_names_size = 0;
1400
9.99M
    return true;
1401
9.99M
  }
1402
1403
273k
      namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1404
273k
      if (namedata == NULL)
1405
58.7k
  return false;
1406
1407
215k
      filesize = bfd_get_file_size (abfd);
1408
215k
      amt = namedata->parsed_size;
1409
215k
      if (amt + 1 == 0 || (filesize != 0 && amt > filesize))
1410
26.2k
  {
1411
26.2k
    bfd_set_error (bfd_error_malformed_archive);
1412
26.2k
    goto byebye;
1413
26.2k
  }
1414
1415
188k
      bfd_ardata (abfd)->extended_names_size = amt;
1416
188k
      bfd_ardata (abfd)->extended_names = (char *) bfd_alloc (abfd, amt + 1);
1417
188k
      if (bfd_ardata (abfd)->extended_names == NULL)
1418
0
  {
1419
29.6k
  byebye:
1420
29.6k
    free (namedata);
1421
29.6k
    bfd_ardata (abfd)->extended_names = NULL;
1422
29.6k
    bfd_ardata (abfd)->extended_names_size = 0;
1423
29.6k
    return false;
1424
0
  }
1425
1426
188k
      if (bfd_read (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1427
3.40k
  {
1428
3.40k
    if (bfd_get_error () != bfd_error_system_call)
1429
3.40k
      bfd_set_error (bfd_error_malformed_archive);
1430
3.40k
    bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1431
3.40k
    bfd_ardata (abfd)->extended_names = NULL;
1432
3.40k
    goto byebye;
1433
3.40k
  }
1434
185k
      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
185k
      {
1442
185k
  char *ext_names = bfd_ardata (abfd)->extended_names;
1443
185k
  char *temp = ext_names;
1444
185k
  char *limit = temp + namedata->parsed_size;
1445
1446
9.20M
  for (; temp < limit; ++temp)
1447
9.01M
    {
1448
9.01M
      if (*temp == ARFMAG[1])
1449
228k
        temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1450
9.01M
      if (*temp == '\\')
1451
8.79k
        *temp = '/';
1452
9.01M
    }
1453
185k
  *limit = '\0';
1454
185k
      }
1455
1456
      /* Pad to an even boundary if you have to.  */
1457
185k
      bfd_ardata (abfd)->first_file.file_offset = bfd_tell (abfd);
1458
185k
      bfd_ardata (abfd)->first_file.file_offset +=
1459
185k
  (bfd_ardata (abfd)->first_file.file_offset) % 2;
1460
1461
185k
      free (namedata);
1462
185k
    }
1463
187k
  return true;
1464
10.2M
}
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.16k
{
1510
1.16k
  if (abfd->flags & BFD_ARCHIVE_FULL_PATH)
1511
0
    return file;
1512
1.16k
  return lbasename (file);
1513
1.16k
}
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
849
{
1652
849
  *name = "ARFILENAMES/";
1653
849
  return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen);
1654
849
}
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
16.2k
{
1664
16.2k
  *name = "//";
1665
16.2k
  return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen);
1666
16.2k
}
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
15
{
1674
15
  return true;
1675
15
}
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
17.0k
{
1691
17.0k
  unsigned int maxname = ar_maxnamelen (abfd);
1692
17.0k
  bfd_size_type total_namelen = 0;
1693
17.0k
  bfd *current;
1694
17.0k
  char *strptr;
1695
17.0k
  const char *last_filename;
1696
17.0k
  long last_stroff;
1697
1698
17.0k
  *tablen = 0;
1699
17.0k
  last_filename = NULL;
1700
1701
  /* Figure out how long the table should be.  */
1702
17.0k
  for (current = abfd->archive_head;
1703
17.6k
       current != NULL;
1704
17.0k
       current = current->archive_next)
1705
597
    {
1706
597
      const char *normal;
1707
597
      unsigned int thislen;
1708
1709
597
      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
597
      normal = normalize (abfd, bfd_get_filename (current));
1746
597
      if (normal == NULL)
1747
0
  return false;
1748
1749
597
      thislen = strlen (normal);
1750
1751
597
      if (thislen > maxname
1752
3
    && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1753
0
  thislen = maxname;
1754
1755
597
      if (thislen > maxname)
1756
3
  {
1757
    /* Add one to leave room for \n.  */
1758
3
    total_namelen += thislen + 1;
1759
3
    if (trailing_slash)
1760
2
      {
1761
        /* Leave room for trailing slash.  */
1762
2
        ++total_namelen;
1763
2
      }
1764
3
  }
1765
594
      else
1766
594
  {
1767
594
    struct ar_hdr *hdr = arch_hdr (current);
1768
594
    if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1769
594
        || (thislen < sizeof hdr->ar_name
1770
594
      && hdr->ar_name[thislen] != ar_padchar (current)))
1771
140
      {
1772
        /* Must have been using extended format even though it
1773
     didn't need to.  Fix it to use normal format.  */
1774
140
        memcpy (hdr->ar_name, normal, thislen);
1775
140
        if (thislen < maxname
1776
6
      || (thislen == maxname && thislen < sizeof hdr->ar_name))
1777
140
    hdr->ar_name[thislen] = ar_padchar (current);
1778
140
      }
1779
594
  }
1780
597
    }
1781
1782
17.0k
  if (total_namelen == 0)
1783
17.0k
    return true;
1784
1785
3
  *tabloc = (char *) bfd_alloc (abfd, total_namelen);
1786
3
  if (*tabloc == NULL)
1787
0
    return false;
1788
1789
3
  *tablen = total_namelen;
1790
3
  strptr = *tabloc;
1791
1792
3
  last_filename = NULL;
1793
3
  last_stroff = 0;
1794
1795
3
  for (current = abfd->archive_head;
1796
7
       current != NULL;
1797
4
       current = current->archive_next)
1798
4
    {
1799
4
      const char *normal;
1800
4
      unsigned int thislen;
1801
4
      long stroff;
1802
4
      const char *filename = bfd_get_filename (current);
1803
1804
4
      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
4
      else
1825
4
  {
1826
4
    normal = normalize (abfd, filename);
1827
4
    if (normal == NULL)
1828
0
      return false;
1829
4
  }
1830
1831
4
      thislen = strlen (normal);
1832
4
      if (thislen > maxname || bfd_is_thin_archive (abfd))
1833
3
  {
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
3
    struct ar_hdr *hdr = arch_hdr (current);
1838
3
    if (normal == last_filename)
1839
0
      stroff = last_stroff;
1840
3
    else
1841
3
      {
1842
3
        last_filename = filename;
1843
3
        stroff = strptr - *tabloc;
1844
3
        last_stroff = stroff;
1845
3
        memcpy (strptr, normal, thislen);
1846
3
        strptr += thislen;
1847
3
        if (trailing_slash)
1848
2
    *strptr++ = '/';
1849
3
        *strptr++ = ARFMAG[1];
1850
3
      }
1851
3
    hdr->ar_name[0] = ar_padchar (current);
1852
3
    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
3
    else
1861
3
      _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1862
3
  }
1863
4
    }
1864
1865
3
  return true;
1866
3
}
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
425
{
1877
425
  unsigned int maxname = ar_maxnamelen (abfd);
1878
425
  bfd *current;
1879
1880
425
  *tablen = 0;
1881
425
  *tabloc = NULL;
1882
425
  *name = NULL;
1883
1884
425
  for (current = abfd->archive_head;
1885
440
       current != NULL;
1886
425
       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
92
      for (len = 0; normal[len]; len++)
1896
77
  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
425
  return true;
1910
425
}
1911

1912
/* Write an archive header.  */
1913
1914
bool
1915
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1916
580
{
1917
580
  struct ar_hdr *hdr = arch_hdr (abfd);
1918
1919
580
  if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1920
0
    return false;
1921
580
  return true;
1922
580
}
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
2
{
1970
2
  return _bfd_bool_bfd_false_error (archive);
1971
2
}
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
621
{
1999
621
  struct stat status;
2000
621
  struct areltdata *ared;
2001
621
  struct ar_hdr *hdr;
2002
621
  size_t amt;
2003
2004
621
  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
621
  else if (stat (filename, &status) != 0)
2015
0
    {
2016
0
      bfd_set_error (bfd_error_system_call);
2017
0
      return NULL;
2018
0
    }
2019
621
  else
2020
621
    {
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
621
      status.st_mtime = bfd_get_current_time (status.st_mtime);
2027
621
    }
2028
2029
  /* If the caller requested that the BFD generate deterministic output,
2030
     fake values for modification time, UID, GID, and file mode.  */
2031
621
  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
621
  amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
2040
621
  ared = (struct areltdata *) bfd_zmalloc (amt);
2041
621
  if (ared == NULL)
2042
0
    return NULL;
2043
621
  hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
2044
2045
  /* ar headers are space padded, not null padded!  */
2046
621
  memset (hdr, ' ', sizeof (struct ar_hdr));
2047
2048
621
  _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
2049
621
        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
621
    _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
2058
621
          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
621
    _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
2067
621
          status.st_gid);
2068
621
  _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
2069
621
        status.st_mode);
2070
621
  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
621
  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
621
  memcpy (hdr->ar_fmag, ARFMAG, 2);
2082
621
  ared->parsed_size = status.st_size;
2083
621
  ared->arch_header = (char *) hdr;
2084
2085
621
  return ared;
2086
621
}
2087
2088
/* Analogous to stat call.  */
2089
2090
int
2091
bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
2092
31.7k
{
2093
31.7k
  struct ar_hdr *hdr;
2094
31.7k
  char *aloser;
2095
2096
31.7k
  if (abfd->arelt_data == NULL)
2097
29.9k
    {
2098
29.9k
      bfd_set_error (bfd_error_invalid_operation);
2099
29.9k
      return -1;
2100
29.9k
    }
2101
2102
1.75k
  hdr = arch_hdr (abfd);
2103
  /* PR 17512: file: 3d9e9fe9.  */
2104
1.75k
  if (hdr == NULL)
2105
0
    return -1;
2106
1.75k
#define foo(arelt, stelt, size)       \
2107
4.07k
  buf->stelt = strtol (hdr->arelt, &aloser, size);  \
2108
4.07k
  if (aloser == hdr->arelt)       \
2109
4.07k
    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.75k
# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
2135
1.75k
#endif
2136
2137
1.75k
  foo (ar_date, st_mtime, 10);
2138
842
  foo2 (ar_uid, st_uid, 10);
2139
745
  foo2 (ar_gid, st_gid, 10);
2140
739
  foo (ar_mode, st_mode, 8);
2141
2142
710
  buf->st_size = arch_eltdata (abfd)->parsed_size;
2143
2144
710
  return 0;
2145
739
}
2146
2147
void
2148
bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2149
544
{
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
544
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2156
544
  size_t length;
2157
544
  const char *filename;
2158
544
  size_t maxlen = ar_maxnamelen (abfd);
2159
2160
544
  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
544
  filename = normalize (abfd, pathname);
2167
544
  if (filename == NULL)
2168
0
    {
2169
      /* FIXME */
2170
0
      abort ();
2171
0
    }
2172
2173
544
  length = strlen (filename);
2174
2175
544
  if (length <= maxlen)
2176
541
    memcpy (hdr->ar_name, filename, length);
2177
2178
  /* Add the padding character if there is room for it.  */
2179
544
  if (length < maxlen
2180
46
      || (length == maxlen && length < sizeof hdr->ar_name))
2181
541
    (hdr->ar_name)[length] = ar_padchar (abfd);
2182
544
}
2183
2184
void
2185
bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2186
68
{
2187
68
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2188
68
  size_t length;
2189
68
  const char *filename = lbasename (pathname);
2190
68
  size_t maxlen = ar_maxnamelen (abfd);
2191
2192
68
  length = strlen (filename);
2193
2194
68
  if (length <= maxlen)
2195
68
    memcpy (hdr->ar_name, filename, length);
2196
0
  else
2197
0
    {
2198
      /* pathname: meet procrustes */
2199
0
      memcpy (hdr->ar_name, filename, maxlen);
2200
0
      length = maxlen;
2201
0
    }
2202
2203
68
  if (length < maxlen)
2204
66
    (hdr->ar_name)[length] = ar_padchar (abfd);
2205
68
}
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
9
{
2249
9
}
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
17.5k
{
2256
17.5k
  bfd *current;
2257
17.5k
  char *etable = NULL;
2258
17.5k
  bfd_size_type elength = 0;
2259
17.5k
  const char *ename = NULL;
2260
17.5k
  bool makemap = bfd_has_map (arch);
2261
  /* If no .o's, don't bother to make a map.  */
2262
17.5k
  bool hasobjects = false;
2263
17.5k
  bfd_size_type wrote;
2264
17.5k
  int tries;
2265
17.5k
  char *armag;
2266
17.5k
  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
17.5k
  for (current = arch->archive_head;
2272
18.1k
       current != NULL;
2273
17.5k
       current = current->archive_next)
2274
621
    {
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
621
      if (bfd_write_p (current))
2281
0
  {
2282
0
    bfd_set_error (bfd_error_invalid_operation);
2283
0
    goto input_err;
2284
0
  }
2285
621
      if (!current->arelt_data)
2286
621
  {
2287
621
    current->arelt_data =
2288
621
      bfd_ar_hdr_from_filesystem (arch, bfd_get_filename (current),
2289
621
          current);
2290
621
    if (!current->arelt_data)
2291
0
      goto input_err;
2292
2293
    /* Put in the file name.  */
2294
621
    BFD_SEND (arch, _bfd_truncate_arname,
2295
621
        (arch, bfd_get_filename (current),
2296
621
         (char *) arch_hdr (current)));
2297
621
  }
2298
2299
621
      if (makemap && ! hasobjects)
2300
502
  {     /* Don't bother if we won't make a map!  */
2301
502
    if ((bfd_check_format (current, bfd_object)))
2302
238
      hasobjects = true;
2303
502
  }
2304
621
    }
2305
2306
17.5k
  if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2307
17.5k
     (arch, &etable, &elength, &ename)))
2308
0
    return false;
2309
2310
17.5k
  if (bfd_seek (arch, 0, SEEK_SET) != 0)
2311
0
    return false;
2312
17.5k
  armag = ARMAG;
2313
17.5k
  if (bfd_is_thin_archive (arch))
2314
225
    armag = ARMAGT;
2315
17.5k
  wrote = bfd_write (armag, SARMAG, arch);
2316
17.5k
  if (wrote != SARMAG)
2317
0
    return false;
2318
2319
17.5k
  if (makemap && hasobjects)
2320
238
    {
2321
238
      if (!_bfd_compute_and_push_armap (arch, (unsigned int) elength, false,
2322
238
          _bfd_write_armap))
2323
17
  return false;
2324
238
    }
2325
2326
17.4k
  if (elength != 0)
2327
3
    {
2328
3
      struct ar_hdr hdr;
2329
2330
3
      memset (&hdr, ' ', sizeof (struct ar_hdr));
2331
3
      memcpy (hdr.ar_name, ename, strlen (ename));
2332
      /* Round size up to even number in archive header.  */
2333
3
      if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2334
3
          (elength + 1) & ~(bfd_size_type) 1))
2335
0
  return false;
2336
3
      memcpy (hdr.ar_fmag, ARFMAG, 2);
2337
3
      if ((bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2338
3
     != sizeof (struct ar_hdr))
2339
3
    || bfd_write (etable, elength, arch) != elength)
2340
0
  return false;
2341
3
      if ((elength % 2) == 1)
2342
1
  {
2343
1
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2344
0
      return false;
2345
1
  }
2346
3
    }
2347
2348
18.0k
#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
17.4k
  buffer = bfd_malloc (AR_WRITE_BUFFERSIZE);
2353
17.4k
  if (buffer == NULL)
2354
0
    goto input_err;
2355
2356
17.4k
  for (current = arch->archive_head;
2357
18.0k
       current != NULL;
2358
17.4k
       current = current->archive_next)
2359
597
    {
2360
597
      bfd_size_type remaining = arelt_size (current);
2361
2362
      /* Write ar header.  */
2363
597
      if (!_bfd_write_ar_hdr (arch, current))
2364
2
  goto input_err;
2365
595
      if (bfd_is_thin_archive (arch))
2366
0
  continue;
2367
595
      if (bfd_seek (current, 0, SEEK_SET) != 0)
2368
0
  goto input_err;
2369
2370
1.18k
      while (remaining)
2371
594
  {
2372
594
    size_t amt = AR_WRITE_BUFFERSIZE;
2373
2374
594
    if (amt > remaining)
2375
594
      amt = remaining;
2376
594
    errno = 0;
2377
594
    if (bfd_read (buffer, amt, current) != amt)
2378
0
      goto input_err;
2379
594
    if (bfd_write (buffer, amt, arch) != amt)
2380
0
      goto input_err;
2381
594
    remaining -= amt;
2382
594
  }
2383
2384
595
      if ((arelt_size (current) % 2) == 1)
2385
100
  {
2386
100
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2387
0
      goto input_err;
2388
100
  }
2389
595
    }
2390
2391
17.4k
  free (buffer);
2392
2393
17.4k
  if (makemap && hasobjects)
2394
221
    {
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
221
      tries = 1;
2402
221
      do
2403
221
  {
2404
221
    if (bfd_update_armap_timestamp (arch))
2405
221
      break;
2406
0
    _bfd_error_handler
2407
0
      (_("warning: writing archive was slow: rewriting timestamp"));
2408
0
  }
2409
221
      while (++tries < 6);
2410
221
    }
2411
2412
17.4k
  return true;
2413
2414
2
 input_err:
2415
2
  bfd_set_input_error (current, bfd_get_error ());
2416
2
  free (buffer);
2417
2
  return false;
2418
17.4k
}
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
223
{
2428
  /* Dunno if this is the best place for this info...  */
2429
223
  if (elength != 0)
2430
0
    elength += sizeof (struct ar_hdr);
2431
223
  elength += elength % 2;
2432
2433
223
  return BFD_SEND (arch, write_armap,
2434
223
       (arch, elength, map, orl_count, stridx));
2435
223
}
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
240
{
2446
240
  char *first_name = NULL;
2447
240
  bfd *current;
2448
240
  struct orl *map = NULL;
2449
240
  unsigned int orl_max = 1024;    /* Fine initial default.  */
2450
240
  unsigned int orl_count = 0;
2451
240
  int stridx = 0;
2452
240
  asymbol **syms = NULL;
2453
240
  long syms_max = 0;
2454
240
  bool ret;
2455
240
  size_t amt;
2456
240
  static bool report_plugin_err = true;
2457
2458
240
  amt = orl_max * sizeof (struct orl);
2459
240
  map = (struct orl *) bfd_malloc (amt);
2460
240
  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
240
  first_name = (char *) bfd_alloc (arch, 1);
2466
240
  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
240
  while (arch->archive_head
2471
240
   && 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
240
  for (current = arch->archive_head;
2476
468
       current != NULL;
2477
240
       current = current->archive_next)
2478
245
    {
2479
245
      if (bfd_check_format (current, bfd_object)
2480
240
    && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2481
73
  {
2482
73
    long storage;
2483
73
    long symcount;
2484
73
    long src_count;
2485
2486
73
    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
73
    storage = bfd_get_symtab_upper_bound (current);
2497
73
    if (storage < 0)
2498
13
      goto error_return;
2499
2500
60
    if (storage != 0)
2501
60
      {
2502
60
        if (storage > syms_max)
2503
60
    {
2504
60
      free (syms);
2505
60
      syms_max = storage;
2506
60
      syms = (asymbol **) bfd_malloc (syms_max);
2507
60
      if (syms == NULL)
2508
0
        goto error_return;
2509
60
    }
2510
60
        symcount = bfd_canonicalize_symtab (current, syms);
2511
60
        if (symcount < 0)
2512
4
    goto error_return;
2513
2514
        /* Now map over all the symbols, picking out the ones we
2515
     want.  */
2516
2.45k
        for (src_count = 0; src_count < symcount; src_count++)
2517
2.40k
    {
2518
2.40k
      flagword flags = (syms[src_count])->flags;
2519
2.40k
      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.40k
      bool include_in_armap
2526
2.40k
        = ((((flags & (BSF_GLOBAL
2527
2.40k
            | BSF_WEAK
2528
2.40k
            | BSF_INDIRECT
2529
2.40k
            | BSF_GNU_UNIQUE)) != 0
2530
515
       || bfd_is_com_section (sec))
2531
1.88k
      && ! bfd_is_und_section (sec))
2532
525
           || bfd_coff_pe_weak_external_has_real_fallback
2533
525
          (bfd_asymbol_bfd (syms[src_count]), syms[src_count]));
2534
2535
2.40k
      if (include_in_armap)
2536
1.87k
        {
2537
1.87k
          bfd_size_type namelen;
2538
1.87k
          struct orl *new_map;
2539
2540
          /* This symbol will go into the archive header.  */
2541
1.87k
          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.87k
          if (bfd_lto_slim_symbol_p (current,
2553
1.87k
             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.87k
          namelen = strlen (syms[src_count]->name) + 1;
2563
1.87k
          map[orl_count].name = bfd_alloc (arch, namelen);
2564
1.87k
          if (map[orl_count].name == NULL)
2565
0
      goto error_return;
2566
1.87k
          memcpy (map[orl_count].name, syms[src_count]->name,
2567
1.87k
            namelen);
2568
1.87k
          map[orl_count].abfd = current;
2569
2570
1.87k
          stridx += namelen;
2571
1.87k
          ++orl_count;
2572
1.87k
        }
2573
2.40k
    }
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
245
    }
2582
2583
  /* OK, now we have collected all the data, let's push them out.  */
2584
223
  ret = push_armap (arch, elength, map, orl_count, stridx);
2585
223
  if (!ret)
2586
0
    goto error_return;
2587
2588
223
  free (syms);
2589
223
  free (map);
2590
223
  if (!keep_symtab)
2591
223
    bfd_release (arch, first_name);
2592
2593
223
  return ret;
2594
2595
17
 error_return:
2596
17
  free (syms);
2597
17
  free (map);
2598
17
  if (first_name != NULL)
2599
17
    bfd_release (arch, first_name);
2600
2601
17
  return false;
2602
223
}
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
17
{
2611
17
  int padit = stridx & 1;
2612
17
  unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2613
17
  unsigned int stringsize = stridx + padit;
2614
  /* Include 8 bytes to store ranlibsize and stringsize in output.  */
2615
17
  unsigned int mapsize = ranlibsize + stringsize + 8;
2616
17
  file_ptr firstreal, first;
2617
17
  bfd *current;
2618
17
  bfd *last_elt;
2619
17
  bfd_byte temp[4];
2620
17
  unsigned int count;
2621
17
  struct ar_hdr hdr;
2622
17
  long uid, gid;
2623
17
  unsigned int namidx = 0;
2624
2625
17
  first = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2626
2627
17
#ifdef BFD64
2628
17
  firstreal = first;
2629
17
  current = arch->archive_head;
2630
17
  last_elt = current; /* Last element arch seen.  */
2631
17
  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
17
#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
17
  bfd_ardata (arch)->armap_timestamp = 0;
2667
17
  uid = 0;
2668
17
  gid = 0;
2669
17
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2670
17
    {
2671
17
      struct stat statbuf;
2672
2673
17
      if (stat (bfd_get_filename (arch), &statbuf) == 0)
2674
17
  {
2675
    /* If asked, replace the time with a deterministic value. */
2676
17
    statbuf.st_mtime = bfd_get_current_time (statbuf.st_mtime);
2677
2678
17
    bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2679
17
            + ARMAP_TIME_OFFSET);
2680
17
  }
2681
17
      uid = getuid();
2682
17
      gid = getgid();
2683
17
    }
2684
2685
17
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2686
17
  memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
2687
17
  bfd_ardata (arch)->armap_datepos = (SARMAG
2688
17
              + offsetof (struct ar_hdr, ar_date[0]));
2689
17
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2690
17
        bfd_ardata (arch)->armap_timestamp);
2691
17
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2692
17
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
2693
17
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2694
0
    return false;
2695
17
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2696
17
  if (bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2697
17
      != sizeof (struct ar_hdr))
2698
0
    return false;
2699
17
  H_PUT_32 (arch, ranlibsize, temp);
2700
17
  if (bfd_write (temp, sizeof (temp), arch) != sizeof (temp))
2701
0
    return false;
2702
2703
17
  firstreal = first;
2704
17
  current = arch->archive_head;
2705
17
  last_elt = current; /* Last element arch seen.  */
2706
17
  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
17
  H_PUT_32 (arch, stringsize, temp);
2747
17
  if (bfd_write (temp, sizeof (temp), arch) != sizeof (temp))
2748
0
    return false;
2749
17
  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
17
  if (padit)
2760
0
    {
2761
0
      if (bfd_write ("", 1, arch) != 1)
2762
0
  return false;
2763
0
    }
2764
2765
17
  return true;
2766
17
}
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
184
{
2778
184
  struct stat archstat;
2779
184
  struct ar_hdr hdr;
2780
2781
  /* If creating deterministic archives, just leave the timestamp as-is.  */
2782
184
  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
184
  bfd_flush (arch);
2788
184
  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
184
  if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2797
    /* OK by the linker's rules.  */
2798
17
    return true;
2799
2800
167
  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
167
  bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2808
2809
  /* Prepare an ASCII version suitable for writing.  */
2810
167
  memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2811
167
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2812
167
        bfd_ardata (arch)->armap_timestamp);
2813
2814
  /* Write it into the file.  */
2815
167
  bfd_ardata (arch)->armap_datepos = (SARMAG
2816
167
              + offsetof (struct ar_hdr, ar_date[0]));
2817
167
  if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2818
167
      || (bfd_write (hdr.ar_date, sizeof (hdr.ar_date), arch)
2819
167
    != 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
167
  return false;
2829
167
}
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
198
{
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
198
  unsigned int ranlibsize = (symbol_count * 4) + 4;
2854
198
  unsigned int stringsize = stridx;
2855
198
  unsigned int mapsize = stringsize + ranlibsize;
2856
198
  file_ptr archive_member_file_ptr;
2857
198
  file_ptr first_archive_member_file_ptr;
2858
198
  bfd *current = arch->archive_head;
2859
198
  unsigned int count;
2860
198
  struct ar_hdr hdr;
2861
198
  int padit = mapsize & 1;
2862
2863
198
  if (padit)
2864
14
    mapsize++;
2865
2866
  /* Work out where the first object file will go in the archive.  */
2867
198
  first_archive_member_file_ptr = (mapsize
2868
198
           + elength
2869
198
           + sizeof (struct ar_hdr)
2870
198
           + SARMAG);
2871
2872
198
#ifdef BFD64
2873
198
  current = arch->archive_head;
2874
198
  count = 0;
2875
198
  archive_member_file_ptr = first_archive_member_file_ptr;
2876
244
  while (current != NULL && count < symbol_count)
2877
46
    {
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.91k
      while (count < symbol_count && map[count].abfd == current)
2882
1.86k
  {
2883
1.86k
    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.86k
    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.86k
    count++;
2891
1.86k
  }
2892
46
      archive_member_file_ptr += sizeof (struct ar_hdr);
2893
46
      if (! bfd_is_thin_archive (arch))
2894
46
  {
2895
    /* Add size of this archive entry.  */
2896
46
    archive_member_file_ptr += arelt_size (current);
2897
    /* Remember about the even alignment.  */
2898
46
    archive_member_file_ptr += archive_member_file_ptr % 2;
2899
46
  }
2900
46
      current = current->archive_next;
2901
46
    }
2902
198
#endif
2903
2904
198
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2905
198
  hdr.ar_name[0] = '/';
2906
198
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2907
0
    return false;
2908
198
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2909
198
        ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2910
198
         ? bfd_get_current_time (0) : 0));
2911
  /* This, at least, is what Intel coff sets the values to.  */
2912
198
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2913
198
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2914
198
  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2915
198
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2916
2917
  /* Write the ar header for this item and the number of symbols.  */
2918
198
  if (bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2919
198
      != sizeof (struct ar_hdr))
2920
0
    return false;
2921
2922
198
  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
198
  current = arch->archive_head;
2932
198
  count = 0;
2933
198
  archive_member_file_ptr = first_archive_member_file_ptr;
2934
244
  while (current != NULL && count < symbol_count)
2935
46
    {
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.91k
      while (count < symbol_count && map[count].abfd == current)
2940
1.86k
  {
2941
1.86k
    unsigned int offset = (unsigned int) archive_member_file_ptr;
2942
2943
    /* Catch an attempt to grow an archive past its 4Gb limit.  */
2944
1.86k
    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.86k
    if (!bfd_write_bigendian_4byte_int (arch, offset))
2950
0
      return false;
2951
1.86k
    count++;
2952
1.86k
  }
2953
46
      archive_member_file_ptr += sizeof (struct ar_hdr);
2954
46
      if (! bfd_is_thin_archive (arch))
2955
46
  {
2956
    /* Add size of this archive entry.  */
2957
46
    archive_member_file_ptr += arelt_size (current);
2958
    /* Remember about the even alignment.  */
2959
46
    archive_member_file_ptr += archive_member_file_ptr % 2;
2960
46
  }
2961
46
      current = current->archive_next;
2962
46
    }
2963
2964
  /* Now write the strings themselves.  */
2965
2.06k
  for (count = 0; count < symbol_count; count++)
2966
1.86k
    {
2967
1.86k
      size_t len = strlen (map[count].name) + 1;
2968
2969
1.86k
      if (bfd_write (map[count].name, len, arch) != len)
2970
0
  return false;
2971
1.86k
    }
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
198
  if (padit)
2976
14
    {
2977
14
      if (bfd_write ("", 1, arch) != 1)
2978
0
  return false;
2979
14
    }
2980
2981
198
  return true;
2982
198
}
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.3M
{
3007
10.3M
  if (arch_eltdata (abfd) != NULL)
3008
10.1M
    {
3009
10.1M
      struct areltdata *ared = arch_eltdata (abfd);
3010
10.1M
      htab_t htab = (htab_t) ared->parent_cache;
3011
3012
10.1M
      if (htab)
3013
17.0k
  {
3014
17.0k
    struct ar_cache ent;
3015
17.0k
    void **slot;
3016
3017
17.0k
    ent.ptr = ared->key;
3018
17.0k
    slot = htab_find_slot (htab, &ent, NO_INSERT);
3019
17.0k
    if (slot != NULL)
3020
17.0k
      {
3021
17.0k
        BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
3022
17.0k
        htab_clear_slot (htab, slot);
3023
17.0k
      }
3024
17.0k
  }
3025
10.1M
    }
3026
10.3M
}
3027
3028
bool
3029
_bfd_archive_close_and_cleanup (bfd *abfd)
3030
10.3M
{
3031
10.3M
  if (bfd_write_p (abfd) && abfd->format == bfd_archive)
3032
20.6k
    {
3033
20.6k
      bfd *current;
3034
23.8k
      while ((current = abfd->archive_head) != NULL)
3035
3.15k
  {
3036
3.15k
    abfd->archive_head = current->archive_next;
3037
3.15k
    bfd_close_all_done (current);
3038
3.15k
  }
3039
20.6k
    }
3040
10.3M
  if (bfd_read_p (abfd) && abfd->format == bfd_archive)
3041
44.7k
    {
3042
44.7k
      bfd *nbfd;
3043
44.7k
      bfd *next;
3044
44.7k
      htab_t htab;
3045
3046
      /* Close nested archives (if this bfd is a thin archive).  */
3047
44.7k
      for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
3048
14
  {
3049
14
    next = nbfd->archive_next;
3050
14
    bfd_close (nbfd);
3051
14
  }
3052
3053
44.7k
      htab = bfd_ardata (abfd)->cache;
3054
44.7k
      if (htab)
3055
8.12k
  {
3056
8.12k
    htab_traverse_noresize (htab, archive_close_worker, NULL);
3057
8.12k
    htab_delete (htab);
3058
8.12k
    bfd_ardata (abfd)->cache = NULL;
3059
8.12k
  }
3060
3061
      /* Close the archive plugin file descriptor if needed.  */
3062
44.7k
      if (abfd->archive_plugin_fd > 0)
3063
0
  close (abfd->archive_plugin_fd);
3064
44.7k
    }
3065
3066
10.3M
  _bfd_unlink_from_archive_parent (abfd);
3067
3068
10.3M
  if (abfd->is_linker_output)
3069
0
    (*abfd->link.hash->hash_table_free) (abfd);
3070
3071
  return true;
3072
10.3M
}