Coverage Report

Created: 2026-03-10 08:46

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