Coverage Report

Created: 2026-05-11 07:54

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
387
#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
217
29.2M
#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
218
219
19.0M
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
220
1.09k
#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
15.6M
  (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
1.20k
{
230
1.20k
  char buf[20];
231
1.20k
  size_t len;
232
233
1.20k
  snprintf (buf, sizeof (buf), fmt, val);
234
1.20k
  len = strlen (buf);
235
1.20k
  if (len < n)
236
845
    {
237
845
      memcpy (p, buf, len);
238
845
      memset (p + len, ' ', n - len);
239
845
    }
240
364
  else
241
364
    memcpy (p, buf, n);
242
1.20k
}
243
244
bool
245
_bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
246
241
{
247
241
  char buf[21];
248
241
  size_t len;
249
250
241
  snprintf (buf, sizeof (buf), "%-10" PRIu64, (uint64_t) size);
251
241
  len = strlen (buf);
252
241
  if (len > n)
253
0
    {
254
0
      bfd_set_error (bfd_error_file_too_big);
255
0
      return false;
256
0
    }
257
241
  if (len < n)
258
0
    {
259
0
      memcpy (p, buf, len);
260
0
      memset (p + len, ' ', n - len);
261
0
    }
262
241
  else
263
241
    memcpy (p, buf, n);
264
241
  return true;
265
241
}
266

267
bool
268
_bfd_generic_mkarchive (bfd *abfd)
269
869
{
270
869
  size_t amt = sizeof (struct artdata);
271
272
869
  abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
273
869
  return bfd_ardata (abfd) != NULL;
274
869
}
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.43M
{
323
9.43M
  return _bfd_new_bfd_contained_in (obfd);
324
9.43M
}
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.45M
{
348
9.45M
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
349
9.45M
  struct ar_cache m;
350
351
9.45M
  m.ptr = filepos;
352
353
9.45M
  if (hash_table)
354
10.4k
    {
355
10.4k
      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
356
10.4k
      if (!entry)
357
10.4k
  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
10.4k
    }
365
9.44M
  else
366
9.44M
    return NULL;
367
9.45M
}
368
369
static hashval_t
370
hash_file_ptr (const void * p)
371
33.3k
{
372
33.3k
  return (hashval_t) (((struct ar_cache *) p)->ptr);
373
33.3k
}
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
11.5k
{
380
11.5k
  struct ar_cache *arc1 = (struct ar_cache *) p1;
381
11.5k
  struct ar_cache *arc2 = (struct ar_cache *) p2;
382
11.5k
  return arc1->ptr == arc2->ptr;
383
11.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
9.15k
{
391
9.15k
  return calloc (a, b);
392
9.15k
}
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
11.3k
{
399
11.3k
  struct ar_cache *cache;
400
11.3k
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
401
402
  /* If the hash table hasn't been created, create it.  */
403
11.3k
  if (hash_table == NULL)
404
4.48k
    {
405
4.48k
      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
406
4.48k
              NULL, _bfd_calloc_wrapper, free);
407
4.48k
      if (hash_table == NULL)
408
0
  return false;
409
4.48k
      bfd_ardata (arch_bfd)->cache = hash_table;
410
4.48k
    }
411
412
  /* Insert new_elt into the hash table by filepos.  */
413
11.3k
  cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
414
11.3k
  cache->ptr = filepos;
415
11.3k
  cache->arbfd = new_elt;
416
11.3k
  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
417
418
  /* Provide a means of accessing this from child.  */
419
11.3k
  arch_eltdata (new_elt)->parent_cache = hash_table;
420
11.3k
  arch_eltdata (new_elt)->key = filepos;
421
422
11.3k
  return true;
423
11.3k
}
424

425
static bfd *
426
open_nested_file (const char *filename, bfd *archive)
427
11.6k
{
428
11.6k
  const char *target;
429
11.6k
  bfd *n_bfd;
430
431
11.6k
  target = NULL;
432
11.6k
  if (!archive->target_defaulted)
433
0
    target = archive->xvec->name;
434
11.6k
  n_bfd = bfd_openr (filename, target);
435
11.6k
  if (n_bfd != NULL)
436
9.76k
    {
437
9.76k
      n_bfd->lto_output = archive->lto_output;
438
9.76k
      n_bfd->no_export = archive->no_export;
439
9.76k
      n_bfd->my_archive = archive;
440
9.76k
    }
441
11.6k
  return n_bfd;
442
11.6k
}
443
444
static bfd *
445
find_nested_archive (const char *filename, bfd *arch_bfd)
446
707
{
447
707
  bfd *abfd;
448
449
  /* PR 15140: Don't allow a nested archive pointing to itself.  */
450
707
  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
707
  for (abfd = arch_bfd->nested_archives;
457
707
       abfd != NULL;
458
707
       abfd = abfd->archive_next)
459
292
    {
460
292
      if (filename_cmp (filename, bfd_get_filename (abfd)) == 0)
461
292
  return abfd;
462
292
    }
463
415
  abfd = open_nested_file (filename, arch_bfd);
464
415
  if (abfd)
465
10
    {
466
10
      abfd->archive_next = arch_bfd->nested_archives;
467
10
      arch_bfd->nested_archives = abfd;
468
10
    }
469
415
  return abfd;
470
707
}
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
12.0k
{
478
12.0k
  unsigned long table_index = 0;
479
12.0k
  const char *endp;
480
481
  /* Should extract string so that I can guarantee not to overflow into
482
     the next region, but I'm too lazy.  */
483
12.0k
  errno = 0;
484
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
485
12.0k
  table_index = strtol (name + 1, (char **) &endp, 10);
486
12.0k
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
487
1.46k
    {
488
1.46k
      bfd_set_error (bfd_error_malformed_archive);
489
1.46k
      return NULL;
490
1.46k
    }
491
  /* In a thin archive, a member of an archive-within-an-archive
492
     will have the offset in the inner archive encoded here.  */
493
10.6k
  if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
494
1.27k
    {
495
1.27k
      file_ptr origin = strtol (endp + 1, NULL, 10);
496
497
1.27k
      if (errno != 0)
498
102
  {
499
102
    bfd_set_error (bfd_error_malformed_archive);
500
102
    return NULL;
501
102
  }
502
1.17k
      *originp = origin;
503
1.17k
    }
504
9.33k
  else
505
9.33k
    *originp = 0;
506
507
10.5k
  return bfd_ardata (arch)->extended_names + table_index;
508
10.6k
}
509
510
/* This functions reads an arch header and returns an areltdata pointer, or
511
   NULL on error.
512
513
   Presumes the file pointer is already in the right place (ie pointing
514
   to the ar_hdr in the file).   Moves the file pointer; on success it
515
   should be pointing to the front of the file contents; on failure it
516
   could have been moved arbitrarily.  */
517
518
void *
519
_bfd_generic_read_ar_hdr (bfd *abfd)
520
15.9M
{
521
15.9M
  return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
522
15.9M
}
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
16.0M
{
530
16.0M
  struct ar_hdr hdr;
531
16.0M
  char *hdrp = (char *) &hdr;
532
16.0M
  uint64_t parsed_size;
533
16.0M
  struct areltdata *ared;
534
16.0M
  char *filename = NULL;
535
16.0M
  ufile_ptr filesize;
536
16.0M
  bfd_size_type namelen = 0;
537
16.0M
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
538
16.0M
  char *allocptr = 0;
539
16.0M
  file_ptr origin = 0;
540
16.0M
  unsigned int extra_size = 0;
541
16.0M
  char fmag_save;
542
16.0M
  int scan;
543
544
16.0M
  if (bfd_read (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
545
23.7k
    {
546
23.7k
      if (bfd_get_error () != bfd_error_system_call)
547
23.7k
  bfd_set_error (bfd_error_no_more_archived_files);
548
23.7k
      return NULL;
549
23.7k
    }
550
16.0M
  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
551
195k
      && (mag == NULL
552
1.10k
    || strncmp (hdr.ar_fmag, mag, 2) != 0))
553
194k
    {
554
194k
      bfd_set_error (bfd_error_malformed_archive);
555
194k
      return NULL;
556
194k
    }
557
558
16.0M
  errno = 0;
559
15.8M
  fmag_save = hdr.ar_fmag[0];
560
15.8M
  hdr.ar_fmag[0] = 0;
561
15.8M
  scan = sscanf (hdr.ar_size, "%" SCNu64, &parsed_size);
562
15.8M
  hdr.ar_fmag[0] = fmag_save;
563
15.8M
  if (scan != 1)
564
160k
    {
565
160k
      bfd_set_error (bfd_error_malformed_archive);
566
160k
      return NULL;
567
160k
    }
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
15.6M
  if ((hdr.ar_name[0] == '/'
573
11.1M
       || (hdr.ar_name[0] == ' '
574
231k
     && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
575
4.66M
      && bfd_ardata (abfd)->extended_names != NULL)
576
12.0k
    {
577
12.0k
      filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
578
12.0k
      if (filename == NULL)
579
1.56k
  return NULL;
580
12.0k
    }
581
  /* BSD4.4-style long filename.  */
582
15.6M
  else if (is_bsd44_extended_name (hdr.ar_name))
583
33.3k
    {
584
      /* BSD-4.4 extended name */
585
33.3k
      namelen = atoi (&hdr.ar_name[3]);
586
33.3k
      filesize = bfd_get_file_size (abfd);
587
33.3k
      if (namelen > parsed_size
588
32.4k
    || namelen > -allocsize - 2
589
32.4k
    || (filesize != 0 && namelen > filesize))
590
1.27k
  {
591
1.27k
    bfd_set_error (bfd_error_malformed_archive);
592
1.27k
    return NULL;
593
1.27k
  }
594
32.0k
      allocsize += namelen + 1;
595
32.0k
      parsed_size -= namelen;
596
32.0k
      extra_size = namelen;
597
598
32.0k
      allocptr = (char *) bfd_malloc (allocsize);
599
32.0k
      if (allocptr == NULL)
600
0
  return NULL;
601
32.0k
      filename = (allocptr
602
32.0k
      + sizeof (struct areltdata)
603
32.0k
      + sizeof (struct ar_hdr));
604
32.0k
      if (bfd_read (filename, namelen, abfd) != namelen)
605
373
  {
606
373
    free (allocptr);
607
373
    if (bfd_get_error () != bfd_error_system_call)
608
373
      bfd_set_error (bfd_error_no_more_archived_files);
609
373
    return NULL;
610
373
  }
611
31.7k
      filename[namelen] = '\0';
612
31.7k
    }
613
15.6M
  else
614
15.6M
    {
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
15.6M
      char *e;
620
15.6M
      e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
621
15.6M
      if (e == NULL)
622
9.04M
  {
623
9.04M
    e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
624
9.04M
    if (e == NULL)
625
3.46M
      e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
626
9.04M
  }
627
628
15.6M
      if (e != NULL)
629
14.6M
  namelen = e - hdr.ar_name;
630
917k
      else
631
917k
  {
632
    /* If we didn't find a termination character, then the name
633
       must be the entire field.  */
634
917k
    namelen = ar_maxnamelen (abfd);
635
917k
  }
636
637
15.6M
      allocsize += namelen + 1;
638
15.6M
    }
639
640
15.6M
  if (!allocptr)
641
15.6M
    {
642
15.6M
      allocptr = (char *) bfd_malloc (allocsize);
643
15.6M
      if (allocptr == NULL)
644
0
  return NULL;
645
15.6M
    }
646
647
15.6M
  memset (allocptr, 0, sizeof (struct areltdata));
648
15.6M
  ared = (struct areltdata *) allocptr;
649
15.6M
  ared->arch_header = allocptr + sizeof (struct areltdata);
650
15.6M
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
651
15.6M
  ared->parsed_size = parsed_size;
652
15.6M
  ared->extra_size = extra_size;
653
15.6M
  ared->origin = origin;
654
655
15.6M
  if (filename != NULL)
656
42.2k
    ared->filename = filename;
657
15.6M
  else
658
15.6M
    {
659
15.6M
      ared->filename = allocptr + (sizeof (struct areltdata) +
660
15.6M
           sizeof (struct ar_hdr));
661
15.6M
      if (namelen)
662
10.5M
  memcpy (ared->filename, hdr.ar_name, namelen);
663
15.6M
      ared->filename[namelen] = '\0';
664
15.6M
    }
665
666
15.6M
  return ared;
667
15.6M
}
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
10.9k
{
675
10.9k
  const char *arch_name = bfd_get_filename (arch);
676
10.9k
  const char *base_name = arch_name != NULL ? lbasename (arch_name) : NULL;
677
10.9k
  size_t prefix_len;
678
10.9k
  char *filename;
679
680
10.9k
  if (base_name == arch_name)
681
0
    return elt_name;
682
683
10.9k
  prefix_len = base_name - arch_name;
684
10.9k
  filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
685
10.9k
  if (filename == NULL)
686
0
    return NULL;
687
688
10.9k
  strncpy (filename, arch_name, prefix_len);
689
10.9k
  strcpy (filename + prefix_len, elt_name);
690
10.9k
  return filename;
691
10.9k
}
692
693
/* This is an internal function; it's mainly used when indexing
694
   through the archive symbol table, but also used to get the next
695
   element, since it handles the bookkeeping so nicely for us.  */
696
697
bfd *
698
_bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos,
699
       struct bfd_link_info *info)
700
9.45M
{
701
9.45M
  struct areltdata *new_areldata;
702
9.45M
  bfd *n_bfd;
703
9.45M
  char *filename;
704
705
9.45M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
706
707
9.45M
  n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos);
708
9.45M
  if (n_bfd)
709
0
    return n_bfd;
710
711
9.45M
  if (0 > bfd_seek (archive, filepos, SEEK_SET))
712
108
    return NULL;
713
714
9.45M
  if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
715
13.0k
    return NULL;
716
717
9.44M
  filename = new_areldata->filename;
718
719
9.44M
  if (bfd_is_thin_archive (archive))
720
11.9k
    {
721
      /* This is a proxy entry for an external file.  */
722
11.9k
      if (! IS_ABSOLUTE_PATH (filename))
723
10.9k
  {
724
10.9k
    filename = _bfd_append_relative_path (archive, filename);
725
10.9k
    if (filename == NULL)
726
0
      {
727
0
        free (new_areldata);
728
0
        return NULL;
729
0
      }
730
10.9k
  }
731
732
11.9k
      if (new_areldata->origin > 0)
733
707
  {
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
707
    bfd *ext_arch = find_nested_archive (filename, archive);
737
707
    file_ptr origin = new_areldata->origin;
738
739
707
    free (new_areldata);
740
707
    if (ext_arch == NULL
741
302
        || ! bfd_check_format (ext_arch, bfd_archive))
742
707
      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
11.2k
      bfd_set_error (bfd_error_no_error);
760
11.2k
      n_bfd = open_nested_file (filename, archive);
761
11.2k
      if (n_bfd == NULL)
762
1.51k
  {
763
1.51k
    switch (bfd_get_error ())
764
1.51k
      {
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.51k
      case bfd_error_system_call:
771
1.51k
        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.51k
        break;
779
1.51k
      }
780
1.51k
  }
781
11.2k
    }
782
9.43M
  else
783
9.43M
    {
784
9.43M
      n_bfd = _bfd_create_empty_archive_element_shell (archive);
785
9.43M
    }
786
787
9.44M
  if (n_bfd == NULL)
788
1.51k
    {
789
1.51k
      free (new_areldata);
790
1.51k
      return NULL;
791
1.51k
    }
792
793
9.44M
  n_bfd->proxy_handle.file_offset = bfd_tell (archive);
794
795
9.44M
  if (bfd_is_thin_archive (archive))
796
9.75k
    {
797
9.75k
      n_bfd->origin = 0;
798
9.75k
    }
799
9.43M
  else
800
9.43M
    {
801
9.43M
      n_bfd->origin = n_bfd->proxy_handle.file_offset;
802
9.43M
      if (!bfd_set_filename (n_bfd, filename))
803
0
  goto out;
804
9.43M
    }
805
806
9.44M
  n_bfd->arelt_data = new_areldata;
807
808
  /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags.  */
809
9.44M
  n_bfd->flags |= archive->flags & (BFD_COMPRESS
810
9.44M
            | BFD_DECOMPRESS
811
9.44M
            | BFD_COMPRESS_GABI);
812
813
  /* Copy is_linker_input.  */
814
9.44M
  n_bfd->is_linker_input = archive->is_linker_input;
815
816
9.44M
  if (archive->no_element_cache
817
11.3k
      || _bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd))
818
9.44M
    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.44M
}
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.46M
{
866
9.46M
  if ((bfd_get_format (archive) != bfd_archive)
867
9.46M
      || (archive->direction == write_direction))
868
0
    {
869
0
      bfd_set_error (bfd_error_invalid_operation);
870
0
      return NULL;
871
0
    }
872
873
9.46M
  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.46M
  else
877
9.46M
    return BFD_SEND (archive,
878
9.46M
         openr_next_archived_file, (archive, last_file));
879
9.46M
}
880
881
bfd *
882
bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
883
9.41M
{
884
9.41M
  ufile_ptr filestart;
885
886
9.41M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
887
888
9.41M
  if (!last_file)
889
9.40M
    filestart = bfd_ardata (archive)->first_file.file_offset;
890
9.28k
  else
891
9.28k
    {
892
9.28k
      filestart = last_file->proxy_handle.file_offset;
893
9.28k
      if (! bfd_is_thin_archive (archive))
894
9.26k
  {
895
9.26k
    bfd_size_type size = arelt_size (last_file);
896
897
9.26k
    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
9.26k
    filestart += filestart % 2;
902
9.26k
    if (filestart < last_file->proxy_handle.file_offset)
903
164
      {
904
        /* Prevent looping.  See PR19256.  */
905
164
        bfd_set_error (bfd_error_malformed_archive);
906
164
        return NULL;
907
164
      }
908
9.26k
  }
909
9.28k
    }
910
911
9.41M
  return _bfd_get_elt_at_filepos (archive, filestart, NULL);
912
9.41M
}
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
25.9M
{
924
25.9M
  char armag[SARMAG + 1];
925
25.9M
  size_t amt;
926
927
25.9M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
928
929
25.9M
  if (bfd_read (armag, SARMAG, abfd) != SARMAG)
930
132k
    {
931
132k
      if (bfd_get_error () != bfd_error_system_call)
932
61.3k
  bfd_set_error (bfd_error_wrong_format);
933
132k
      return NULL;
934
132k
    }
935
936
25.8M
  bfd_set_thin_archive (abfd, strncmp (armag, ARMAGT, SARMAG) == 0);
937
938
25.8M
  if (strncmp (armag, ARMAG, SARMAG) != 0
939
15.2M
      && ! bfd_is_thin_archive (abfd))
940
15.1M
    {
941
15.1M
      bfd_set_error (bfd_error_wrong_format);
942
15.1M
      return NULL;
943
15.1M
    }
944
945
10.7M
  amt = sizeof (struct artdata);
946
10.7M
  bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
947
10.7M
  if (bfd_ardata (abfd) == NULL)
948
0
    return NULL;
949
950
10.7M
  bfd_ardata (abfd)->first_file.file_offset = SARMAG;
951
952
10.7M
  if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
953
9.52M
      || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
954
1.29M
    {
955
1.29M
      if (bfd_get_error () != bfd_error_system_call)
956
1.29M
  bfd_set_error (bfd_error_wrong_format);
957
1.29M
      bfd_release (abfd, bfd_ardata (abfd));
958
1.29M
      return NULL;
959
1.29M
    }
960
961
9.44M
  if (abfd->target_defaulted || abfd->is_linker_input)
962
9.44M
    {
963
9.44M
      bfd *first;
964
9.44M
      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.44M
      save = abfd->no_element_cache;
977
9.44M
      abfd->no_element_cache = 1;
978
9.44M
      first = bfd_openr_next_archived_file (abfd, NULL);
979
9.44M
      abfd->no_element_cache = save;
980
9.44M
      if (first != NULL)
981
9.43M
  {
982
9.43M
    first->target_defaulted = false;
983
9.43M
    if (abfd->is_linker_input)
984
0
      first->plugin_format = bfd_plugin_no;
985
9.43M
    if (!bfd_check_format (first, bfd_object)
986
51.6k
        || first->xvec != abfd->xvec)
987
9.37M
      bfd_set_error (bfd_error_wrong_object_format);
988
9.43M
    bfd_close (first);
989
9.43M
  }
990
9.44M
    }
991
992
9.44M
  return _bfd_no_cleanup;
993
10.7M
}
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
4.81M
#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
7.32M
#define BSD_SYMDEF_COUNT_SIZE 4
1092
1093
/* The size of the string count.  */
1094
7.32M
#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
2.74M
{
1102
2.74M
  struct areltdata *mapdata;
1103
2.74M
  size_t counter;
1104
2.74M
  bfd_byte *raw_armap, *rbase;
1105
2.74M
  struct artdata *ardata = bfd_ardata (abfd);
1106
2.74M
  char *stringbase;
1107
2.74M
  bfd_size_type parsed_size;
1108
2.74M
  size_t amt, string_size;
1109
2.74M
  carsym *set;
1110
1111
2.74M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1112
1113
2.74M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1114
2.74M
  if (mapdata == NULL)
1115
83.0k
    return false;
1116
2.66M
  parsed_size = mapdata->parsed_size;
1117
2.66M
  free (mapdata);
1118
  /* PR 17512: file: 883ff754.  */
1119
  /* PR 17512: file: 0458885f.  */
1120
2.66M
  if (parsed_size < BSD_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE)
1121
1.15k
    {
1122
1.15k
      bfd_set_error (bfd_error_malformed_archive);
1123
1.15k
      return false;
1124
1.15k
    }
1125
1126
2.65M
  raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, parsed_size, parsed_size);
1127
2.65M
  if (raw_armap == NULL)
1128
74.0k
    return false;
1129
1130
2.58M
  parsed_size -= BSD_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE;
1131
2.58M
  amt = H_GET_32 (abfd, raw_armap);
1132
2.58M
  if (amt > parsed_size
1133
2.08M
      || amt % BSD_SYMDEF_SIZE != 0)
1134
503k
    {
1135
      /* Probably we're using the wrong byte ordering.  */
1136
503k
      bfd_set_error (bfd_error_wrong_format);
1137
503k
      goto release_armap;
1138
503k
    }
1139
1140
2.08M
  rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
1141
2.08M
  stringbase = (char *) rbase + amt + BSD_STRING_COUNT_SIZE;
1142
2.08M
  string_size = parsed_size - amt;
1143
1144
2.08M
  ardata->symdef_count = amt / BSD_SYMDEF_SIZE;
1145
2.08M
  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
2.08M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1151
2.08M
  if (!ardata->symdefs)
1152
0
    goto release_armap;
1153
1154
2.08M
  for (counter = 0, set = ardata->symdefs;
1155
2.73M
       counter < ardata->symdef_count;
1156
2.08M
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
1157
656k
    {
1158
656k
      unsigned nameoff = H_GET_32 (abfd, rbase);
1159
656k
      if (nameoff >= string_size)
1160
8.07k
  {
1161
8.07k
    bfd_set_error (bfd_error_malformed_archive);
1162
8.07k
    goto release_armap;
1163
8.07k
  }
1164
648k
      set->name = stringbase + nameoff;
1165
648k
      set->u.file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1166
648k
    }
1167
1168
2.07M
  ardata->first_file.file_offset = bfd_tell (abfd);
1169
  /* Pad to an even boundary if you have to.  */
1170
2.07M
  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
2.07M
  abfd->has_armap = true;
1175
2.07M
  return true;
1176
1177
511k
 release_armap:
1178
511k
  ardata->symdef_count = 0;
1179
511k
  ardata->symdefs = NULL;
1180
511k
  bfd_release (abfd, raw_armap);
1181
511k
  return false;
1182
2.08M
}
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
1.83M
{
1190
1.83M
  struct areltdata *mapdata;
1191
1.83M
  int *raw_armap, *rawptr;
1192
1.83M
  struct artdata *ardata = bfd_ardata (abfd);
1193
1.83M
  char *stringbase;
1194
1.83M
  char *stringend;
1195
1.83M
  bfd_size_type stringsize;
1196
1.83M
  bfd_size_type parsed_size;
1197
1.83M
  ufile_ptr filesize;
1198
1.83M
  size_t nsymz, carsym_size, ptrsize, i;
1199
1.83M
  carsym *carsyms;
1200
1.83M
  bfd_vma (*swap) (const void *);
1201
1.83M
  char int_buf[4];
1202
1.83M
  struct areltdata *tmp;
1203
1204
1.83M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1205
1206
1.83M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1207
1.83M
  if (mapdata == NULL)
1208
219k
    return false;
1209
1.61M
  parsed_size = mapdata->parsed_size;
1210
1.61M
  free (mapdata);
1211
1212
1.61M
  if (bfd_read (int_buf, 4, abfd) != 4)
1213
1.63k
    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
1.61M
  swap = bfd_getb32;
1218
1.61M
  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
1.61M
  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
1.61M
  filesize = bfd_get_file_size (abfd);
1230
1.61M
  ptrsize = 4 * nsymz;
1231
1.61M
  if ((filesize != 0 && parsed_size > filesize)
1232
1.59M
      || parsed_size < 4
1233
1.59M
      || parsed_size - 4 < ptrsize)
1234
34.5k
    {
1235
34.5k
      bfd_set_error (bfd_error_malformed_archive);
1236
34.5k
      return false;
1237
34.5k
    }
1238
1239
1.58M
  stringsize = parsed_size - ptrsize - 4;
1240
1241
1.58M
  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
1.58M
  raw_armap = (int *) _bfd_malloc_and_read (abfd, ptrsize, ptrsize);
1249
1.58M
  if (raw_armap == NULL)
1250
2.81k
    return false;
1251
1252
1.57M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd,
1253
1.57M
             carsym_size + stringsize + 1);
1254
1.57M
  if (ardata->symdefs == NULL)
1255
0
    goto free_armap;
1256
1.57M
  carsyms = ardata->symdefs;
1257
1.57M
  stringbase = ((char *) ardata->symdefs) + carsym_size;
1258
1259
1.57M
  if (bfd_read (stringbase, stringsize, abfd) != stringsize)
1260
3.04k
    goto release_symdefs;
1261
1262
  /* OK, build the carsyms.  */
1263
1.57M
  stringend = stringbase + stringsize;
1264
1.57M
  *stringend = 0;
1265
5.35M
  for (i = 0; i < nsymz; i++)
1266
3.78M
    {
1267
3.78M
      rawptr = raw_armap + i;
1268
3.78M
      carsyms->u.file_offset = swap ((bfd_byte *) rawptr);
1269
3.78M
      carsyms->name = stringbase;
1270
3.78M
      stringbase += strlen (stringbase);
1271
3.78M
      if (stringbase != stringend)
1272
3.14M
  ++stringbase;
1273
3.78M
      carsyms++;
1274
3.78M
    }
1275
1276
1.57M
  ardata->symdef_count = nsymz;
1277
1.57M
  ardata->first_file.file_offset = bfd_tell (abfd);
1278
  /* Pad to an even boundary if you have to.  */
1279
1.57M
  ardata->first_file.file_offset += (ardata->first_file.file_offset) % 2;
1280
1.57M
  if (bfd_seek (abfd, ardata->first_file.file_offset, SEEK_SET) != 0)
1281
0
    goto release_symdefs;
1282
1283
1.57M
  abfd->has_armap = true;
1284
1.57M
  free (raw_armap);
1285
1286
  /* Check for a second archive header (as used by PE).  */
1287
1.57M
  tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1288
1.57M
  if (tmp != NULL)
1289
1.57M
    {
1290
1.57M
      if (tmp->arch_header[0] == '/'
1291
147k
    && tmp->arch_header[1] == ' ')
1292
5.43k
  ardata->first_file.file_offset
1293
5.43k
    += (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1294
1.57M
      free (tmp);
1295
1.57M
    }
1296
1297
1.57M
  return true;
1298
1299
3.04k
 release_symdefs:
1300
3.04k
  bfd_release (abfd, (ardata)->symdefs);
1301
3.04k
 free_armap:
1302
3.04k
  free (raw_armap);
1303
3.04k
  return false;
1304
3.04k
}
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.5M
{
1312
10.5M
  char nextname[16];
1313
10.5M
  int i = bfd_read (nextname, 16, abfd);
1314
1315
10.5M
  if (i == 0)
1316
6
    return true;
1317
10.5M
  if (i != 16)
1318
4.60k
    return false;
1319
1320
10.5M
  if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1321
0
    return false;
1322
1323
10.5M
  if (memcmp (nextname, "__.SYMDEF       ", 16) == 0
1324
      /* Old Linux archives.  */
1325
8.95M
      || memcmp (nextname, "__.SYMDEF/      ", 16) == 0)
1326
2.73M
    return do_slurp_bsd_armap (abfd);
1327
7.80M
  else if (memcmp (nextname, "/               ", 16) == 0)
1328
1.83M
    return do_slurp_coff_armap (abfd);
1329
5.96M
  else if (memcmp (nextname, "/SYM64/         ", 16) == 0)
1330
128k
    {
1331
      /* 64bit (Irix 6) archive.  */
1332
128k
#ifdef BFD64
1333
128k
      return _bfd_archive_64_bit_slurp_armap (abfd);
1334
#else
1335
      bfd_set_error (bfd_error_wrong_format);
1336
      return false;
1337
#endif
1338
128k
    }
1339
5.83M
  else if (memcmp (nextname, "________", 8) == 0
1340
280k
     && ((nextname[8] == '_' && nextname[9] == '_')
1341
159k
         || (nextname[8] == '6' && nextname[9] == '4'))
1342
224k
     && nextname[10] == 'E'
1343
182k
     && (nextname[11] == 'B' || nextname[11] == 'L')
1344
171k
     && nextname[12] == 'E'
1345
162k
     && (nextname[13] == 'B' || nextname[13] == 'L')
1346
153k
     && nextname[14] == '_'
1347
147k
     && (nextname[15] == ' ' || nextname[15] == 'X'))
1348
142k
    {
1349
      /* ECOFF archive.  */
1350
142k
      bfd_set_error (bfd_error_wrong_format);
1351
142k
      return false;
1352
142k
    }
1353
5.69M
  else if (memcmp (nextname, "#1/20           ", 16) == 0)
1354
29.5k
    {
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
29.5k
      struct ar_hdr hdr;
1359
29.5k
      char extname[20];
1360
1361
29.5k
      if (bfd_read (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1362
4.37k
  return false;
1363
      /* Read the extended name.  We know its length.  */
1364
25.1k
      if (bfd_read (extname, 20, abfd) != 20)
1365
11.0k
  return false;
1366
14.1k
      if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1367
0
  return false;
1368
14.1k
      if (memcmp (extname, "__.SYMDEF SORTED", 16) == 0
1369
13.2k
    || memcmp (extname, "__.SYMDEF", 9) == 0)
1370
4.83k
  return do_slurp_bsd_armap (abfd);
1371
14.1k
    }
1372
1373
5.67M
  abfd->has_armap = false;
1374
5.67M
  return true;
1375
10.5M
}
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.52M
{
1392
9.52M
  char nextname[17];
1393
1394
9.52M
  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.52M
  if (bfd_seek (abfd, bfd_ardata (abfd)->first_file.file_offset,
1399
9.52M
    SEEK_SET) != 0)
1400
0
    return false;
1401
1402
9.52M
  if (bfd_read (nextname, 16, abfd) == 16)
1403
9.52M
    {
1404
9.52M
      struct areltdata *namedata;
1405
9.52M
      bfd_size_type amt;
1406
9.52M
      ufile_ptr filesize;
1407
1408
9.52M
      if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1409
0
  return false;
1410
1411
9.52M
      if (! startswith (nextname, "ARFILENAMES/    ")
1412
9.44M
    && ! startswith (nextname, "//              "))
1413
9.24M
  {
1414
9.24M
    bfd_ardata (abfd)->extended_names = NULL;
1415
9.24M
    bfd_ardata (abfd)->extended_names_size = 0;
1416
9.24M
    return true;
1417
9.24M
  }
1418
1419
285k
      namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1420
285k
      if (namedata == NULL)
1421
53.6k
  return false;
1422
1423
231k
      filesize = bfd_get_file_size (abfd);
1424
231k
      amt = namedata->parsed_size;
1425
231k
      if (amt + 1 == 0 || (filesize != 0 && amt > filesize))
1426
28.1k
  {
1427
28.1k
    bfd_set_error (bfd_error_malformed_archive);
1428
28.1k
    goto byebye;
1429
28.1k
  }
1430
1431
203k
      bfd_ardata (abfd)->extended_names_size = amt;
1432
203k
      bfd_ardata (abfd)->extended_names = (char *) bfd_alloc (abfd, amt + 1);
1433
203k
      if (bfd_ardata (abfd)->extended_names == NULL)
1434
0
  {
1435
31.4k
  byebye:
1436
31.4k
    free (namedata);
1437
31.4k
    bfd_ardata (abfd)->extended_names = NULL;
1438
31.4k
    bfd_ardata (abfd)->extended_names_size = 0;
1439
31.4k
    return false;
1440
0
  }
1441
1442
203k
      if (bfd_read (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1443
3.30k
  {
1444
3.30k
    if (bfd_get_error () != bfd_error_system_call)
1445
3.30k
      bfd_set_error (bfd_error_malformed_archive);
1446
3.30k
    bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1447
3.30k
    bfd_ardata (abfd)->extended_names = NULL;
1448
3.30k
    goto byebye;
1449
3.30k
  }
1450
200k
      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
200k
      {
1458
200k
  char *ext_names = bfd_ardata (abfd)->extended_names;
1459
200k
  char *temp = ext_names;
1460
200k
  char *limit = temp + namedata->parsed_size;
1461
1462
9.08M
  for (; temp < limit; ++temp)
1463
8.88M
    {
1464
8.88M
      if (*temp == ARFMAG[1])
1465
323k
        temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1466
8.88M
      if (*temp == '\\')
1467
5.82k
        *temp = '/';
1468
8.88M
    }
1469
200k
  *limit = '\0';
1470
200k
      }
1471
1472
      /* Pad to an even boundary if you have to.  */
1473
200k
      bfd_ardata (abfd)->first_file.file_offset = bfd_tell (abfd);
1474
200k
      bfd_ardata (abfd)->first_file.file_offset +=
1475
200k
  (bfd_ardata (abfd)->first_file.file_offset) % 2;
1476
1477
200k
      free (namedata);
1478
200k
    }
1479
201k
  return true;
1480
9.52M
}
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
363
{
1526
363
  if (abfd->flags & BFD_ARCHIVE_FULL_PATH)
1527
0
    return file;
1528
363
  return lbasename (file);
1529
363
}
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
800
{
1668
800
  *name = "ARFILENAMES/";
1669
800
  return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen);
1670
800
}
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
16.5k
{
1680
16.5k
  *name = "//";
1681
16.5k
  return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen);
1682
16.5k
}
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
15
{
1690
15
  return true;
1691
15
}
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
17.3k
{
1707
17.3k
  unsigned int maxname = ar_maxnamelen (abfd);
1708
17.3k
  bfd_size_type total_namelen = 0;
1709
17.3k
  bfd *current;
1710
17.3k
  char *strptr;
1711
17.3k
  const char *last_filename;
1712
17.3k
  long last_stroff;
1713
1714
17.3k
  *tablen = 0;
1715
17.3k
  last_filename = NULL;
1716
1717
  /* Figure out how long the table should be.  */
1718
17.3k
  for (current = abfd->archive_head;
1719
17.5k
       current != NULL;
1720
17.3k
       current = current->archive_next)
1721
181
    {
1722
181
      const char *normal;
1723
181
      unsigned int thislen;
1724
1725
181
      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
181
      normal = normalize (abfd, bfd_get_filename (current));
1762
181
      if (normal == NULL)
1763
0
  return false;
1764
1765
181
      thislen = strlen (normal);
1766
1767
181
      if (thislen > maxname
1768
0
    && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1769
0
  thislen = maxname;
1770
1771
181
      if (thislen > maxname)
1772
0
  {
1773
    /* Add one to leave room for \n.  */
1774
0
    total_namelen += thislen + 1;
1775
0
    if (trailing_slash)
1776
0
      {
1777
        /* Leave room for trailing slash.  */
1778
0
        ++total_namelen;
1779
0
      }
1780
0
  }
1781
181
      else
1782
181
  {
1783
181
    struct ar_hdr *hdr = arch_hdr (current);
1784
181
    if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1785
181
        || (thislen < sizeof hdr->ar_name
1786
181
      && hdr->ar_name[thislen] != ar_padchar (current)))
1787
24
      {
1788
        /* Must have been using extended format even though it
1789
     didn't need to.  Fix it to use normal format.  */
1790
24
        memcpy (hdr->ar_name, normal, thislen);
1791
24
        if (thislen < maxname
1792
0
      || (thislen == maxname && thislen < sizeof hdr->ar_name))
1793
24
    hdr->ar_name[thislen] = ar_padchar (current);
1794
24
      }
1795
181
  }
1796
181
    }
1797
1798
17.3k
  if (total_namelen == 0)
1799
17.3k
    return true;
1800
1801
0
  *tabloc = (char *) bfd_alloc (abfd, total_namelen);
1802
0
  if (*tabloc == NULL)
1803
0
    return false;
1804
1805
0
  *tablen = total_namelen;
1806
0
  strptr = *tabloc;
1807
1808
0
  last_filename = NULL;
1809
0
  last_stroff = 0;
1810
1811
0
  for (current = abfd->archive_head;
1812
0
       current != NULL;
1813
0
       current = current->archive_next)
1814
0
    {
1815
0
      const char *normal;
1816
0
      unsigned int thislen;
1817
0
      long stroff;
1818
0
      const char *filename = bfd_get_filename (current);
1819
1820
0
      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
0
      else
1841
0
  {
1842
0
    normal = normalize (abfd, filename);
1843
0
    if (normal == NULL)
1844
0
      return false;
1845
0
  }
1846
1847
0
      thislen = strlen (normal);
1848
0
      if (thislen > maxname || bfd_is_thin_archive (abfd))
1849
0
  {
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
0
    struct ar_hdr *hdr = arch_hdr (current);
1854
0
    if (normal == last_filename)
1855
0
      stroff = last_stroff;
1856
0
    else
1857
0
      {
1858
0
        last_filename = filename;
1859
0
        stroff = strptr - *tabloc;
1860
0
        last_stroff = stroff;
1861
0
        memcpy (strptr, normal, thislen);
1862
0
        strptr += thislen;
1863
0
        if (trailing_slash)
1864
0
    *strptr++ = '/';
1865
0
        *strptr++ = ARFMAG[1];
1866
0
      }
1867
0
    hdr->ar_name[0] = ar_padchar (current);
1868
0
    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
0
    else
1877
0
      _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1878
0
  }
1879
0
    }
1880
1881
0
  return true;
1882
0
}
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
391
{
1893
391
  unsigned int maxname = ar_maxnamelen (abfd);
1894
391
  bfd *current;
1895
1896
391
  *tablen = 0;
1897
391
  *tabloc = NULL;
1898
391
  *name = NULL;
1899
1900
391
  for (current = abfd->archive_head;
1901
392
       current != NULL;
1902
391
       current = current->archive_next)
1903
1
    {
1904
1
      const char *normal = normalize (abfd, bfd_get_filename (current));
1905
1
      int has_space = 0;
1906
1
      unsigned int len;
1907
1908
1
      if (normal == NULL)
1909
0
  return false;
1910
1911
2
      for (len = 0; normal[len]; len++)
1912
1
  if (normal[len] == ' ')
1913
0
    has_space = 1;
1914
1915
1
      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
1
    }
1924
1925
391
  return true;
1926
391
}
1927

1928
/* Write an archive header.  */
1929
1930
bool
1931
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1932
172
{
1933
172
  struct ar_hdr *hdr = arch_hdr (abfd);
1934
1935
172
  if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1936
0
    return false;
1937
172
  return true;
1938
172
}
1939
1940
/* Write an archive header using BSD4.4 convention.  */
1941
1942
bool
1943
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1944
1
{
1945
1
  struct ar_hdr *hdr = arch_hdr (abfd);
1946
1947
1
  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
1
  else
1976
1
    {
1977
1
      if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1978
0
  return false;
1979
1
    }
1980
1
  return true;
1981
1
}
1982
1983
bool
1984
_bfd_noarchive_write_ar_hdr (bfd *archive, bfd *abfd ATTRIBUTE_UNUSED)
1985
0
{
1986
0
  return _bfd_bool_bfd_false_error (archive);
1987
0
}
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
182
{
2015
182
  struct stat status;
2016
182
  struct areltdata *ared;
2017
182
  struct ar_hdr *hdr;
2018
182
  size_t amt;
2019
2020
182
  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
182
  else if (stat (filename, &status) != 0)
2031
0
    {
2032
0
      bfd_set_error (bfd_error_system_call);
2033
0
      return NULL;
2034
0
    }
2035
182
  else
2036
182
    {
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
182
      status.st_mtime = bfd_get_current_time (status.st_mtime);
2043
182
    }
2044
2045
  /* If the caller requested that the BFD generate deterministic output,
2046
     fake values for modification time, UID, GID, and file mode.  */
2047
182
  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
182
  amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
2056
182
  ared = (struct areltdata *) bfd_zmalloc (amt);
2057
182
  if (ared == NULL)
2058
0
    return NULL;
2059
182
  hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
2060
2061
  /* ar headers are space padded, not null padded!  */
2062
182
  memset (hdr, ' ', sizeof (struct ar_hdr));
2063
2064
182
  _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
2065
182
        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
182
    _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
2074
182
          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
182
    _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
2083
182
          status.st_gid);
2084
182
  _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
2085
182
        status.st_mode);
2086
182
  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
182
  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
182
  memcpy (hdr->ar_fmag, ARFMAG, 2);
2098
182
  ared->parsed_size = status.st_size;
2099
182
  ared->arch_header = (char *) hdr;
2100
2101
182
  return ared;
2102
182
}
2103
2104
/* Analogous to stat call.  */
2105
2106
int
2107
bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
2108
14.5k
{
2109
14.5k
  struct ar_hdr *hdr;
2110
14.5k
  char *aloser;
2111
2112
14.5k
  if (abfd->arelt_data == NULL)
2113
13.8k
    {
2114
13.8k
      bfd_set_error (bfd_error_invalid_operation);
2115
13.8k
      return -1;
2116
13.8k
    }
2117
2118
737
  hdr = arch_hdr (abfd);
2119
  /* PR 17512: file: 3d9e9fe9.  */
2120
737
  if (hdr == NULL)
2121
0
    return -1;
2122
737
#define foo(arelt, stelt, size)       \
2123
1.88k
  buf->stelt = strtol (hdr->arelt, &aloser, size);  \
2124
1.88k
  if (aloser == hdr->arelt)       \
2125
1.88k
    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
768
# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
2151
737
#endif
2152
2153
737
  foo (ar_date, st_mtime, 10);
2154
389
  foo2 (ar_uid, st_uid, 10);
2155
379
  foo2 (ar_gid, st_gid, 10);
2156
377
  foo (ar_mode, st_mode, 8);
2157
2158
368
  buf->st_size = arch_eltdata (abfd)->parsed_size;
2159
2160
368
  return 0;
2161
377
}
2162
2163
void
2164
bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2165
181
{
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
181
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2172
181
  size_t length;
2173
181
  const char *filename;
2174
181
  size_t maxlen = ar_maxnamelen (abfd);
2175
2176
181
  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
181
  filename = normalize (abfd, pathname);
2183
181
  if (filename == NULL)
2184
0
    {
2185
      /* FIXME */
2186
0
      abort ();
2187
0
    }
2188
2189
181
  length = strlen (filename);
2190
2191
181
  if (length <= maxlen)
2192
181
    memcpy (hdr->ar_name, filename, length);
2193
2194
  /* Add the padding character if there is room for it.  */
2195
181
  if (length < maxlen
2196
0
      || (length == maxlen && length < sizeof hdr->ar_name))
2197
181
    (hdr->ar_name)[length] = ar_padchar (abfd);
2198
181
}
2199
2200
void
2201
bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2202
1
{
2203
1
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2204
1
  size_t length;
2205
1
  const char *filename = lbasename (pathname);
2206
1
  size_t maxlen = ar_maxnamelen (abfd);
2207
2208
1
  length = strlen (filename);
2209
2210
1
  if (length <= maxlen)
2211
1
    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
1
  if (length < maxlen)
2220
1
    (hdr->ar_name)[length] = ar_padchar (abfd);
2221
1
}
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
0
{
2265
0
}
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.7k
{
2272
17.7k
  bfd *current;
2273
17.7k
  char *etable = NULL;
2274
17.7k
  bfd_size_type elength = 0;
2275
17.7k
  const char *ename = NULL;
2276
17.7k
  bool makemap = bfd_has_map (arch);
2277
  /* If no .o's, don't bother to make a map.  */
2278
17.7k
  bool hasobjects = false;
2279
17.7k
  bfd_size_type wrote;
2280
17.7k
  int tries;
2281
17.7k
  char *armag;
2282
17.7k
  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.7k
  for (current = arch->archive_head;
2288
17.9k
       current != NULL;
2289
17.7k
       current = current->archive_next)
2290
182
    {
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
182
      if (bfd_write_p (current))
2297
0
  {
2298
0
    bfd_set_error (bfd_error_invalid_operation);
2299
0
    goto input_err;
2300
0
  }
2301
182
      if (!current->arelt_data)
2302
182
  {
2303
182
    current->arelt_data =
2304
182
      bfd_ar_hdr_from_filesystem (arch, bfd_get_filename (current),
2305
182
          current);
2306
182
    if (!current->arelt_data)
2307
0
      goto input_err;
2308
2309
    /* Put in the file name.  */
2310
182
    BFD_SEND (arch, _bfd_truncate_arname,
2311
182
        (arch, bfd_get_filename (current),
2312
182
         (char *) arch_hdr (current)));
2313
182
  }
2314
2315
182
      if (makemap && ! hasobjects)
2316
160
  {     /* Don't bother if we won't make a map!  */
2317
160
    if ((bfd_check_format (current, bfd_object)))
2318
67
      hasobjects = true;
2319
160
  }
2320
182
    }
2321
2322
17.7k
  if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2323
17.7k
     (arch, &etable, &elength, &ename)))
2324
0
    return false;
2325
2326
17.7k
  if (bfd_seek (arch, 0, SEEK_SET) != 0)
2327
0
    return false;
2328
17.7k
  armag = ARMAG;
2329
17.7k
  if (bfd_is_thin_archive (arch))
2330
217
    armag = ARMAGT;
2331
17.7k
  wrote = bfd_write (armag, SARMAG, arch);
2332
17.7k
  if (wrote != SARMAG)
2333
0
    return false;
2334
2335
17.7k
  if (makemap && hasobjects)
2336
67
    {
2337
67
      if (!_bfd_compute_and_push_armap (arch, (unsigned int) elength, false,
2338
67
          _bfd_write_armap))
2339
8
  return false;
2340
67
    }
2341
2342
17.7k
  if (elength != 0)
2343
0
    {
2344
0
      struct ar_hdr hdr;
2345
2346
0
      memset (&hdr, ' ', sizeof (struct ar_hdr));
2347
0
      memcpy (hdr.ar_name, ename, strlen (ename));
2348
      /* Round size up to even number in archive header.  */
2349
0
      if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2350
0
          (elength + 1) & ~(bfd_size_type) 1))
2351
0
  return false;
2352
0
      memcpy (hdr.ar_fmag, ARFMAG, 2);
2353
0
      if ((bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2354
0
     != sizeof (struct ar_hdr))
2355
0
    || bfd_write (etable, elength, arch) != elength)
2356
0
  return false;
2357
0
      if ((elength % 2) == 1)
2358
0
  {
2359
0
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2360
0
      return false;
2361
0
  }
2362
0
    }
2363
2364
17.9k
#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.7k
  buffer = bfd_malloc (AR_WRITE_BUFFERSIZE);
2369
17.7k
  if (buffer == NULL)
2370
0
    goto input_err;
2371
2372
17.7k
  for (current = arch->archive_head;
2373
17.9k
       current != NULL;
2374
17.7k
       current = current->archive_next)
2375
173
    {
2376
173
      bfd_size_type remaining = arelt_size (current);
2377
2378
      /* Write ar header.  */
2379
173
      if (!_bfd_write_ar_hdr (arch, current))
2380
0
  goto input_err;
2381
173
      if (bfd_is_thin_archive (arch))
2382
0
  continue;
2383
173
      if (bfd_seek (current, 0, SEEK_SET) != 0)
2384
0
  goto input_err;
2385
2386
346
      while (remaining)
2387
173
  {
2388
173
    size_t amt = AR_WRITE_BUFFERSIZE;
2389
2390
173
    if (amt > remaining)
2391
173
      amt = remaining;
2392
173
    errno = 0;
2393
173
    if (bfd_read (buffer, amt, current) != amt)
2394
0
      goto input_err;
2395
173
    if (bfd_write (buffer, amt, arch) != amt)
2396
0
      goto input_err;
2397
173
    remaining -= amt;
2398
173
  }
2399
2400
173
      if ((arelt_size (current) % 2) == 1)
2401
4
  {
2402
4
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2403
0
      goto input_err;
2404
4
  }
2405
173
    }
2406
2407
17.7k
  free (buffer);
2408
2409
17.7k
  if (makemap && hasobjects)
2410
59
    {
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
59
      tries = 1;
2418
59
      do
2419
59
  {
2420
59
    if (bfd_update_armap_timestamp (arch))
2421
59
      break;
2422
0
    _bfd_error_handler
2423
0
      (_("warning: writing archive was slow: rewriting timestamp"));
2424
0
  }
2425
59
      while (++tries < 6);
2426
59
    }
2427
2428
17.7k
  return true;
2429
2430
0
 input_err:
2431
0
  bfd_set_input_error (current, bfd_get_error ());
2432
0
  free (buffer);
2433
0
  return false;
2434
17.7k
}
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
59
{
2444
  /* Dunno if this is the best place for this info...  */
2445
59
  if (elength != 0)
2446
0
    elength += sizeof (struct ar_hdr);
2447
59
  elength += elength % 2;
2448
2449
59
  return BFD_SEND (arch, write_armap,
2450
59
       (arch, elength, map, orl_count, stridx));
2451
59
}
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
67
{
2463
67
  char *first_name = NULL;
2464
67
  bfd *current;
2465
67
  struct orl *map = NULL;
2466
67
  unsigned int orl_max = 1024;    /* Fine initial default.  */
2467
67
  unsigned int orl_count = 0;
2468
67
  int stridx = 0;
2469
67
  asymbol **syms = NULL;
2470
67
  long syms_max = 0;
2471
67
  bool ret;
2472
67
  size_t amt;
2473
67
  static bool report_plugin_err = true;
2474
2475
67
  amt = orl_max * sizeof (struct orl);
2476
67
  map = (struct orl *) bfd_malloc (amt);
2477
67
  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
67
  first_name = (char *) bfd_alloc (arch, 1);
2483
67
  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
67
  while (arch->archive_head
2488
67
   && 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
67
  for (current = arch->archive_head;
2493
127
       current != NULL;
2494
67
       current = current->archive_next)
2495
68
    {
2496
68
      if (bfd_check_format (current, bfd_object)
2497
67
    && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2498
36
  {
2499
36
    long storage;
2500
36
    long symcount;
2501
36
    long src_count;
2502
2503
36
    if (bfd_get_lto_type (current) == lto_slim_ir_object
2504
2
        && !bfd_plugin_target_p (current->xvec)
2505
2
        && report_plugin_err)
2506
1
      {
2507
1
        report_plugin_err = false;
2508
1
        _bfd_error_handler
2509
1
    (_("%pB: plugin needed to handle lto object"),
2510
1
     current);
2511
1
      }
2512
2513
36
    storage = bfd_get_symtab_upper_bound (current);
2514
36
    if (storage < 0)
2515
2
      goto error_return;
2516
2517
34
    if (storage != 0)
2518
34
      {
2519
34
        if (storage > syms_max)
2520
34
    {
2521
34
      free (syms);
2522
34
      syms_max = storage;
2523
34
      syms = (asymbol **) bfd_malloc (syms_max);
2524
34
      if (syms == NULL)
2525
0
        goto error_return;
2526
34
    }
2527
34
        symcount = bfd_canonicalize_symtab (current, syms);
2528
34
        if (symcount < 0)
2529
6
    goto error_return;
2530
2531
        /* Now map over all the symbols, picking out the ones we
2532
     want.  */
2533
1.52k
        for (src_count = 0; src_count < symcount; src_count++)
2534
1.50k
    {
2535
1.50k
      flagword flags = (syms[src_count])->flags;
2536
1.50k
      asection *sec = syms[src_count]->section;
2537
2538
1.50k
      if (((flags & (BSF_GLOBAL
2539
1.50k
         | BSF_WEAK
2540
1.50k
         | BSF_INDIRECT
2541
1.50k
         | BSF_GNU_UNIQUE)) != 0
2542
1.11k
           || bfd_is_com_section (sec))
2543
382
          && ! bfd_is_und_section (sec))
2544
363
        {
2545
363
          bfd_size_type namelen;
2546
363
          struct orl *new_map;
2547
2548
          /* This symbol will go into the archive header.  */
2549
363
          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
363
          if (bfd_lto_slim_symbol_p (current,
2561
363
             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
363
          namelen = strlen (syms[src_count]->name);
2571
363
          amt = sizeof (char *);
2572
363
          map[orl_count].name = (char **) bfd_alloc (arch, amt);
2573
363
          if (map[orl_count].name == NULL)
2574
0
      goto error_return;
2575
363
          *(map[orl_count].name) = (char *) bfd_alloc (arch,
2576
363
                   namelen + 1);
2577
363
          if (*(map[orl_count].name) == NULL)
2578
0
      goto error_return;
2579
363
          strcpy (*(map[orl_count].name), syms[src_count]->name);
2580
363
          map[orl_count].abfd = current;
2581
363
          map[orl_count].namidx = stridx;
2582
2583
363
          stridx += namelen + 1;
2584
363
          ++orl_count;
2585
363
        }
2586
1.50k
    }
2587
28
      }
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
28
    if (!keep_symtab && !bfd_free_cached_info (current))
2592
0
      goto error_return;
2593
28
  }
2594
68
    }
2595
2596
  /* OK, now we have collected all the data, let's push them out.  */
2597
59
  ret = push_armap (arch, elength, map, orl_count, stridx);
2598
2599
59
  free (syms);
2600
59
  free (map);
2601
59
  if (first_name != NULL)
2602
59
    bfd_release (arch, first_name);
2603
2604
59
  return ret;
2605
2606
8
 error_return:
2607
8
  free (syms);
2608
8
  free (map);
2609
8
  if (first_name != NULL)
2610
8
    bfd_release (arch, first_name);
2611
2612
8
  return false;
2613
67
}
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
1
{
2622
1
  int padit = stridx & 1;
2623
1
  unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
2624
1
  unsigned int stringsize = stridx + padit;
2625
  /* Include 8 bytes to store ranlibsize and stringsize in output.  */
2626
1
  unsigned int mapsize = ranlibsize + stringsize + 8;
2627
1
  file_ptr firstreal, first;
2628
1
  bfd *current;
2629
1
  bfd *last_elt;
2630
1
  bfd_byte temp[4];
2631
1
  unsigned int count;
2632
1
  struct ar_hdr hdr;
2633
1
  long uid, gid;
2634
2635
1
  first = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2636
2637
1
#ifdef BFD64
2638
1
  firstreal = first;
2639
1
  current = arch->archive_head;
2640
1
  last_elt = current; /* Last element arch seen.  */
2641
1
  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
1
#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
1
  bfd_ardata (arch)->armap_timestamp = 0;
2677
1
  uid = 0;
2678
1
  gid = 0;
2679
1
  if ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0)
2680
1
    {
2681
1
      struct stat statbuf;
2682
2683
1
      if (stat (bfd_get_filename (arch), &statbuf) == 0)
2684
1
  {
2685
    /* If asked, replace the time with a deterministic value. */
2686
1
    statbuf.st_mtime = bfd_get_current_time (statbuf.st_mtime);
2687
2688
1
    bfd_ardata (arch)->armap_timestamp = (statbuf.st_mtime
2689
1
            + ARMAP_TIME_OFFSET);
2690
1
  }
2691
1
      uid = getuid();
2692
1
      gid = getgid();
2693
1
    }
2694
2695
1
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2696
1
  memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
2697
1
  bfd_ardata (arch)->armap_datepos = (SARMAG
2698
1
              + offsetof (struct ar_hdr, ar_date[0]));
2699
1
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2700
1
        bfd_ardata (arch)->armap_timestamp);
2701
1
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", uid);
2702
1
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", gid);
2703
1
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2704
0
    return false;
2705
1
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2706
1
  if (bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2707
1
      != sizeof (struct ar_hdr))
2708
0
    return false;
2709
1
  H_PUT_32 (arch, ranlibsize, temp);
2710
1
  if (bfd_write (temp, sizeof (temp), arch) != sizeof (temp))
2711
0
    return false;
2712
2713
1
  firstreal = first;
2714
1
  current = arch->archive_head;
2715
1
  last_elt = current; /* Last element arch seen.  */
2716
1
  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
1
  H_PUT_32 (arch, stringsize, temp);
2755
1
  if (bfd_write (temp, sizeof (temp), arch) != sizeof (temp))
2756
0
    return false;
2757
1
  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
1
  if (padit)
2768
0
    {
2769
0
      if (bfd_write ("", 1, arch) != 1)
2770
0
  return false;
2771
0
    }
2772
2773
1
  return true;
2774
1
}
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
247
{
2786
247
  struct stat archstat;
2787
247
  struct ar_hdr hdr;
2788
2789
  /* If creating deterministic archives, just leave the timestamp as-is.  */
2790
247
  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
247
  bfd_flush (arch);
2796
247
  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
247
  if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2805
    /* OK by the linker's rules.  */
2806
1
    return true;
2807
2808
246
  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
246
  bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2816
2817
  /* Prepare an ASCII version suitable for writing.  */
2818
246
  memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2819
246
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2820
246
        bfd_ardata (arch)->armap_timestamp);
2821
2822
  /* Write it into the file.  */
2823
246
  bfd_ardata (arch)->armap_datepos = (SARMAG
2824
246
              + offsetof (struct ar_hdr, ar_date[0]));
2825
246
  if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2826
246
      || (bfd_write (hdr.ar_date, sizeof (hdr.ar_date), arch)
2827
246
    != 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
246
  return false;
2837
246
}
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
58
{
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
58
  unsigned int ranlibsize = (symbol_count * 4) + 4;
2862
58
  unsigned int stringsize = stridx;
2863
58
  unsigned int mapsize = stringsize + ranlibsize;
2864
58
  file_ptr archive_member_file_ptr;
2865
58
  file_ptr first_archive_member_file_ptr;
2866
58
  bfd *current = arch->archive_head;
2867
58
  unsigned int count;
2868
58
  struct ar_hdr hdr;
2869
58
  int padit = mapsize & 1;
2870
2871
58
  if (padit)
2872
7
    mapsize++;
2873
2874
  /* Work out where the first object file will go in the archive.  */
2875
58
  first_archive_member_file_ptr = (mapsize
2876
58
           + elength
2877
58
           + sizeof (struct ar_hdr)
2878
58
           + SARMAG);
2879
2880
58
#ifdef BFD64
2881
58
  current = arch->archive_head;
2882
58
  count = 0;
2883
58
  archive_member_file_ptr = first_archive_member_file_ptr;
2884
72
  while (current != NULL && count < symbol_count)
2885
14
    {
2886
      /* For each symbol which is used defined in this object, write
2887
   out the object file's address in the archive.  */
2888
2889
377
      while (count < symbol_count && map[count].abfd == current)
2890
363
  {
2891
363
    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
363
    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
363
    count++;
2899
363
  }
2900
14
      archive_member_file_ptr += sizeof (struct ar_hdr);
2901
14
      if (! bfd_is_thin_archive (arch))
2902
14
  {
2903
    /* Add size of this archive entry.  */
2904
14
    archive_member_file_ptr += arelt_size (current);
2905
    /* Remember about the even alignment.  */
2906
14
    archive_member_file_ptr += archive_member_file_ptr % 2;
2907
14
  }
2908
14
      current = current->archive_next;
2909
14
    }
2910
58
#endif
2911
2912
58
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2913
58
  hdr.ar_name[0] = '/';
2914
58
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2915
0
    return false;
2916
58
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2917
58
        ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2918
58
         ? bfd_get_current_time (0) : 0));
2919
  /* This, at least, is what Intel coff sets the values to.  */
2920
58
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2921
58
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2922
58
  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2923
58
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2924
2925
  /* Write the ar header for this item and the number of symbols.  */
2926
58
  if (bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2927
58
      != sizeof (struct ar_hdr))
2928
0
    return false;
2929
2930
58
  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
58
  current = arch->archive_head;
2940
58
  count = 0;
2941
58
  archive_member_file_ptr = first_archive_member_file_ptr;
2942
72
  while (current != NULL && count < symbol_count)
2943
14
    {
2944
      /* For each symbol which is used defined in this object, write
2945
   out the object file's address in the archive.  */
2946
2947
377
      while (count < symbol_count && map[count].abfd == current)
2948
363
  {
2949
363
    unsigned int offset = (unsigned int) archive_member_file_ptr;
2950
2951
    /* Catch an attempt to grow an archive past its 4Gb limit.  */
2952
363
    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
363
    if (!bfd_write_bigendian_4byte_int (arch, offset))
2958
0
      return false;
2959
363
    count++;
2960
363
  }
2961
14
      archive_member_file_ptr += sizeof (struct ar_hdr);
2962
14
      if (! bfd_is_thin_archive (arch))
2963
14
  {
2964
    /* Add size of this archive entry.  */
2965
14
    archive_member_file_ptr += arelt_size (current);
2966
    /* Remember about the even alignment.  */
2967
14
    archive_member_file_ptr += archive_member_file_ptr % 2;
2968
14
  }
2969
14
      current = current->archive_next;
2970
14
    }
2971
2972
  /* Now write the strings themselves.  */
2973
421
  for (count = 0; count < symbol_count; count++)
2974
363
    {
2975
363
      size_t len = strlen (*map[count].name) + 1;
2976
2977
363
      if (bfd_write (*map[count].name, len, arch) != len)
2978
0
  return false;
2979
363
    }
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
58
  if (padit)
2984
7
    {
2985
7
      if (bfd_write ("", 1, arch) != 1)
2986
0
  return false;
2987
7
    }
2988
2989
58
  return true;
2990
58
}
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.59M
{
3015
9.59M
  if (arch_eltdata (abfd) != NULL)
3016
9.44M
    {
3017
9.44M
      struct areltdata *ared = arch_eltdata (abfd);
3018
9.44M
      htab_t htab = (htab_t) ared->parent_cache;
3019
3020
9.44M
      if (htab)
3021
11.3k
  {
3022
11.3k
    struct ar_cache ent;
3023
11.3k
    void **slot;
3024
3025
11.3k
    ent.ptr = ared->key;
3026
11.3k
    slot = htab_find_slot (htab, &ent, NO_INSERT);
3027
11.3k
    if (slot != NULL)
3028
11.3k
      {
3029
11.3k
        BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
3030
11.3k
        htab_clear_slot (htab, slot);
3031
11.3k
      }
3032
11.3k
  }
3033
9.44M
    }
3034
9.59M
}
3035
3036
bool
3037
_bfd_archive_close_and_cleanup (bfd *abfd)
3038
9.59M
{
3039
9.59M
  if (bfd_write_p (abfd) && abfd->format == bfd_archive)
3040
18.5k
    {
3041
18.5k
      bfd *current;
3042
19.5k
      while ((current = abfd->archive_head) != NULL)
3043
1.03k
  {
3044
1.03k
    abfd->archive_head = current->archive_next;
3045
1.03k
    bfd_close_all_done (current);
3046
1.03k
  }
3047
18.5k
    }
3048
9.59M
  if (bfd_read_p (abfd) && abfd->format == bfd_archive)
3049
41.2k
    {
3050
41.2k
      bfd *nbfd;
3051
41.2k
      bfd *next;
3052
41.2k
      htab_t htab;
3053
3054
      /* Close nested archives (if this bfd is a thin archive).  */
3055
41.2k
      for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
3056
6
  {
3057
6
    next = nbfd->archive_next;
3058
6
    bfd_close (nbfd);
3059
6
  }
3060
3061
41.2k
      htab = bfd_ardata (abfd)->cache;
3062
41.2k
      if (htab)
3063
4.48k
  {
3064
4.48k
    htab_traverse_noresize (htab, archive_close_worker, NULL);
3065
4.48k
    htab_delete (htab);
3066
4.48k
    bfd_ardata (abfd)->cache = NULL;
3067
4.48k
  }
3068
3069
      /* Close the archive plugin file descriptor if needed.  */
3070
41.2k
      if (abfd->archive_plugin_fd > 0)
3071
0
  close (abfd->archive_plugin_fd);
3072
41.2k
    }
3073
3074
9.59M
  _bfd_unlink_from_archive_parent (abfd);
3075
3076
9.59M
  if (abfd->is_linker_output)
3077
0
    (*abfd->link.hash->hash_table_free) (abfd);
3078
3079
  return true;
3080
9.59M
}