Coverage Report

Created: 2026-07-25 10:20

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