Coverage Report

Created: 2026-04-04 08:16

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