Coverage Report

Created: 2026-10-02 09:53

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