Coverage Report

Created: 2026-07-12 09:22

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.18k
#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
218
27.6M
#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
219
220
19.6M
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
221
2.64k
#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
14.8M
  (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.20k
{
231
3.20k
  char buf[20];
232
3.20k
  size_t len;
233
234
3.20k
  snprintf (buf, sizeof (buf), fmt, val);
235
3.20k
  len = strlen (buf);
236
3.20k
  if (len < n)
237
2.09k
    {
238
2.09k
      memcpy (p, buf, len);
239
2.09k
      memset (p + len, ' ', n - len);
240
2.09k
    }
241
1.11k
  else
242
1.11k
    memcpy (p, buf, n);
243
3.20k
}
244
245
bool
246
_bfd_ar_sizepad (char *p, size_t n, bfd_size_type size)
247
748
{
248
748
  char buf[21];
249
748
  size_t len;
250
251
748
  snprintf (buf, sizeof (buf), "%-10" PRIu64, (uint64_t) size);
252
748
  len = strlen (buf);
253
748
  if (len > n)
254
0
    {
255
0
      bfd_set_error (bfd_error_file_too_big);
256
0
      return false;
257
0
    }
258
748
  if (len < n)
259
0
    {
260
0
      memcpy (p, buf, len);
261
0
      memset (p + len, ' ', n - len);
262
0
    }
263
748
  else
264
748
    memcpy (p, buf, n);
265
748
  return true;
266
748
}
267

268
bool
269
_bfd_generic_mkarchive (bfd *abfd)
270
3.70k
{
271
3.70k
  size_t amt = sizeof (struct artdata);
272
273
3.70k
  abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
274
3.70k
  return bfd_ardata (abfd) != NULL;
275
3.70k
}
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
9.70M
{
324
9.70M
  return _bfd_new_bfd_contained_in (obfd);
325
9.70M
}
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
9.73M
{
349
9.73M
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
350
9.73M
  struct ar_cache m;
351
352
9.73M
  m.ptr = filepos;
353
354
9.73M
  if (hash_table)
355
10.8k
    {
356
10.8k
      struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
357
10.8k
      if (!entry)
358
10.8k
  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
10.8k
    }
366
9.72M
  else
367
9.72M
    return NULL;
368
9.73M
}
369
370
static hashval_t
371
hash_file_ptr (const void * p)
372
38.9k
{
373
38.9k
  return (hashval_t) (((struct ar_cache *) p)->ptr);
374
38.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
14.1k
{
381
14.1k
  struct ar_cache *arc1 = (struct ar_cache *) p1;
382
14.1k
  struct ar_cache *arc2 = (struct ar_cache *) p2;
383
14.1k
  return arc1->ptr == arc2->ptr;
384
14.1k
}
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
14.3k
{
392
14.3k
  return calloc (a, b);
393
14.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
13.9k
{
400
13.9k
  struct ar_cache *cache;
401
13.9k
  htab_t hash_table = bfd_ardata (arch_bfd)->cache;
402
403
  /* If the hash table hasn't been created, create it.  */
404
13.9k
  if (hash_table == NULL)
405
7.11k
    {
406
7.11k
      hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
407
7.11k
              NULL, _bfd_calloc_wrapper, free);
408
7.11k
      if (hash_table == NULL)
409
0
  return false;
410
7.11k
      bfd_ardata (arch_bfd)->cache = hash_table;
411
7.11k
    }
412
413
  /* Insert new_elt into the hash table by filepos.  */
414
13.9k
  cache = (struct ar_cache *) bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
415
13.9k
  cache->ptr = filepos;
416
13.9k
  cache->arbfd = new_elt;
417
13.9k
  *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
418
419
  /* Provide a means of accessing this from child.  */
420
13.9k
  arch_eltdata (new_elt)->parent_cache = hash_table;
421
13.9k
  arch_eltdata (new_elt)->key = filepos;
422
423
13.9k
  return true;
424
13.9k
}
425

426
static bfd *
427
open_nested_file (const char *filename, bfd *archive)
428
13.9k
{
429
13.9k
  const char *target;
430
13.9k
  bfd *n_bfd;
431
432
13.9k
  target = NULL;
433
13.9k
  if (!archive->target_defaulted)
434
0
    target = archive->xvec->name;
435
13.9k
  n_bfd = bfd_openr (filename, target);
436
13.9k
  if (n_bfd != NULL)
437
11.4k
    {
438
11.4k
      n_bfd->lto_output = archive->lto_output;
439
11.4k
      n_bfd->no_export = archive->no_export;
440
11.4k
      n_bfd->my_archive = archive;
441
11.4k
    }
442
13.9k
  return n_bfd;
443
13.9k
}
444
445
static bfd *
446
find_nested_archive (const char *filename, bfd *arch_bfd)
447
716
{
448
716
  bfd *abfd;
449
450
  /* PR 15140: Don't allow a nested archive pointing to itself.  */
451
716
  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
716
  for (abfd = arch_bfd->nested_archives;
458
716
       abfd != NULL;
459
716
       abfd = abfd->archive_next)
460
292
    {
461
292
      if (filename_cmp (filename, bfd_get_filename (abfd)) == 0)
462
292
  return abfd;
463
292
    }
464
424
  abfd = open_nested_file (filename, arch_bfd);
465
424
  if (abfd)
466
16
    {
467
16
      abfd->archive_next = arch_bfd->nested_archives;
468
16
      arch_bfd->nested_archives = abfd;
469
16
    }
470
424
  return abfd;
471
716
}
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
11.2k
{
479
11.2k
  unsigned long table_index = 0;
480
11.2k
  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
11.2k
  errno = 0;
485
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
486
11.2k
  table_index = strtol (name + 1, (char **) &endp, 10);
487
11.2k
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
488
1.33k
    {
489
1.33k
      bfd_set_error (bfd_error_malformed_archive);
490
1.33k
      return NULL;
491
1.33k
    }
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
9.87k
  if (bfd_is_thin_archive (arch) && endp != NULL && *endp == ':')
495
1.29k
    {
496
1.29k
      file_ptr origin = strtol (endp + 1, NULL, 10);
497
498
1.29k
      if (errno != 0)
499
101
  {
500
101
    bfd_set_error (bfd_error_malformed_archive);
501
101
    return NULL;
502
101
  }
503
1.18k
      *originp = origin;
504
1.18k
    }
505
8.58k
  else
506
8.58k
    *originp = 0;
507
508
9.77k
  return bfd_ardata (arch)->extended_names + table_index;
509
9.87k
}
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
15.2M
{
522
15.2M
  return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
523
15.2M
}
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
15.2M
{
531
15.2M
  struct ar_hdr hdr;
532
15.2M
  char *hdrp = (char *) &hdr;
533
15.2M
  uint64_t parsed_size;
534
15.2M
  struct areltdata *ared;
535
15.2M
  char *filename = NULL;
536
15.2M
  ufile_ptr filesize;
537
15.2M
  bfd_size_type namelen = 0;
538
15.2M
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
539
15.2M
  char *allocptr = 0;
540
15.2M
  file_ptr origin = 0;
541
15.2M
  unsigned int extra_size = 0;
542
15.2M
  char fmag_save;
543
15.2M
  int scan;
544
545
15.2M
  if (bfd_read (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
546
27.8k
    {
547
27.8k
      if (bfd_get_error () != bfd_error_system_call)
548
27.8k
  bfd_set_error (bfd_error_no_more_archived_files);
549
27.8k
      return NULL;
550
27.8k
    }
551
15.2M
  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
552
203k
      && (mag == NULL
553
1.24k
    || strncmp (hdr.ar_fmag, mag, 2) != 0))
554
202k
    {
555
202k
      bfd_set_error (bfd_error_malformed_archive);
556
202k
      return NULL;
557
202k
    }
558
559
15.2M
  errno = 0;
560
15.0M
  fmag_save = hdr.ar_fmag[0];
561
15.0M
  hdr.ar_fmag[0] = 0;
562
15.0M
  scan = sscanf (hdr.ar_size, "%" SCNu64, &parsed_size);
563
15.0M
  hdr.ar_fmag[0] = fmag_save;
564
15.0M
  if (scan != 1)
565
168k
    {
566
168k
      bfd_set_error (bfd_error_malformed_archive);
567
168k
      return NULL;
568
168k
    }
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
14.8M
  if ((hdr.ar_name[0] == '/'
574
10.5M
       || (hdr.ar_name[0] == ' '
575
198k
     && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
576
4.48M
      && bfd_ardata (abfd)->extended_names != NULL)
577
11.2k
    {
578
11.2k
      filename = get_extended_arelt_filename (abfd, hdr.ar_name, &origin);
579
11.2k
      if (filename == NULL)
580
1.43k
  return NULL;
581
11.2k
    }
582
  /* BSD4.4-style long filename.  */
583
14.8M
  else if (is_bsd44_extended_name (hdr.ar_name))
584
34.3k
    {
585
      /* BSD-4.4 extended name */
586
34.3k
      namelen = atoi (&hdr.ar_name[3]);
587
34.3k
      filesize = bfd_get_file_size (abfd);
588
34.3k
      if (namelen > parsed_size
589
33.0k
    || namelen > -allocsize - 2
590
32.9k
    || (filesize != 0 && namelen > filesize))
591
1.76k
  {
592
1.76k
    bfd_set_error (bfd_error_malformed_archive);
593
1.76k
    return NULL;
594
1.76k
  }
595
32.5k
      allocsize += namelen + 1;
596
32.5k
      parsed_size -= namelen;
597
32.5k
      extra_size = namelen;
598
599
32.5k
      allocptr = (char *) bfd_malloc (allocsize);
600
32.5k
      if (allocptr == NULL)
601
0
  return NULL;
602
32.5k
      filename = (allocptr
603
32.5k
      + sizeof (struct areltdata)
604
32.5k
      + sizeof (struct ar_hdr));
605
32.5k
      if (bfd_read (filename, namelen, abfd) != namelen)
606
470
  {
607
470
    free (allocptr);
608
470
    if (bfd_get_error () != bfd_error_system_call)
609
470
      bfd_set_error (bfd_error_no_more_archived_files);
610
470
    return NULL;
611
470
  }
612
32.0k
      filename[namelen] = '\0';
613
32.0k
    }
614
14.8M
  else
615
14.8M
    {
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
14.8M
      char *e;
621
14.8M
      e = (char *) memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
622
14.8M
      if (e == NULL)
623
8.43M
  {
624
8.43M
    e = (char *) memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
625
8.43M
    if (e == NULL)
626
3.25M
      e = (char *) memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
627
8.43M
  }
628
629
14.8M
      if (e != NULL)
630
13.9M
  namelen = e - hdr.ar_name;
631
889k
      else
632
889k
  {
633
    /* If we didn't find a termination character, then the name
634
       must be the entire field.  */
635
889k
    namelen = ar_maxnamelen (abfd);
636
889k
  }
637
638
14.8M
      allocsize += namelen + 1;
639
14.8M
    }
640
641
14.8M
  if (!allocptr)
642
14.8M
    {
643
14.8M
      allocptr = (char *) bfd_malloc (allocsize);
644
14.8M
      if (allocptr == NULL)
645
0
  return NULL;
646
14.8M
    }
647
648
14.8M
  memset (allocptr, 0, sizeof (struct areltdata));
649
14.8M
  ared = (struct areltdata *) allocptr;
650
14.8M
  ared->arch_header = allocptr + sizeof (struct areltdata);
651
14.8M
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
652
14.8M
  ared->parsed_size = parsed_size;
653
14.8M
  ared->extra_size = extra_size;
654
14.8M
  ared->origin = origin;
655
656
14.8M
  if (filename != NULL)
657
41.8k
    ared->filename = filename;
658
14.8M
  else
659
14.8M
    {
660
14.8M
      ared->filename = allocptr + (sizeof (struct areltdata) +
661
14.8M
           sizeof (struct ar_hdr));
662
14.8M
      if (namelen)
663
10.4M
  memcpy (ared->filename, hdr.ar_name, namelen);
664
14.8M
      ared->filename[namelen] = '\0';
665
14.8M
    }
666
667
14.8M
  return ared;
668
14.8M
}
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
12.8k
{
676
12.8k
  const char *arch_name = bfd_get_filename (arch);
677
12.8k
  const char *base_name = arch_name != NULL ? lbasename (arch_name) : NULL;
678
12.8k
  size_t prefix_len;
679
12.8k
  char *filename;
680
681
12.8k
  if (base_name == arch_name)
682
0
    return elt_name;
683
684
12.8k
  prefix_len = base_name - arch_name;
685
12.8k
  filename = (char *) bfd_alloc (arch, prefix_len + strlen (elt_name) + 1);
686
12.8k
  if (filename == NULL)
687
0
    return NULL;
688
689
12.8k
  strncpy (filename, arch_name, prefix_len);
690
12.8k
  strcpy (filename + prefix_len, elt_name);
691
12.8k
  return filename;
692
12.8k
}
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
9.73M
{
702
9.73M
  struct areltdata *new_areldata;
703
9.73M
  bfd *n_bfd;
704
9.73M
  char *filename;
705
706
9.73M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
707
708
9.73M
  n_bfd = _bfd_look_for_bfd_in_cache (archive, filepos);
709
9.73M
  if (n_bfd)
710
0
    return n_bfd;
711
712
9.73M
  if (0 > bfd_seek (archive, filepos, SEEK_SET))
713
153
    return NULL;
714
715
9.73M
  if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
716
15.3k
    return NULL;
717
718
9.72M
  filename = new_areldata->filename;
719
720
9.72M
  if (bfd_is_thin_archive (archive))
721
14.2k
    {
722
      /* This is a proxy entry for an external file.  */
723
14.2k
      if (! IS_ABSOLUTE_PATH (filename))
724
12.8k
  {
725
12.8k
    filename = _bfd_append_relative_path (archive, filename);
726
12.8k
    if (filename == NULL)
727
0
      {
728
0
        free (new_areldata);
729
0
        return NULL;
730
0
      }
731
12.8k
  }
732
733
14.2k
      if (new_areldata->origin > 0)
734
716
  {
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
716
    bfd *ext_arch = find_nested_archive (filename, archive);
738
716
    file_ptr origin = new_areldata->origin;
739
740
716
    free (new_areldata);
741
716
    if (ext_arch == NULL
742
308
        || ! bfd_check_format (ext_arch, bfd_archive))
743
716
      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
13.5k
      bfd_set_error (bfd_error_no_error);
761
13.5k
      n_bfd = open_nested_file (filename, archive);
762
13.5k
      if (n_bfd == NULL)
763
2.11k
  {
764
2.11k
    switch (bfd_get_error ())
765
2.11k
      {
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.11k
      case bfd_error_system_call:
772
2.11k
        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.11k
        break;
780
2.11k
      }
781
2.11k
  }
782
13.5k
    }
783
9.70M
  else
784
9.70M
    {
785
9.70M
      n_bfd = _bfd_create_empty_archive_element_shell (archive);
786
9.70M
    }
787
788
9.71M
  if (n_bfd == NULL)
789
2.11k
    {
790
2.11k
      free (new_areldata);
791
2.11k
      return NULL;
792
2.11k
    }
793
794
9.71M
  n_bfd->proxy_handle.file_offset = bfd_tell (archive);
795
796
9.71M
  if (bfd_is_thin_archive (archive))
797
11.4k
    {
798
11.4k
      n_bfd->origin = 0;
799
11.4k
    }
800
9.70M
  else
801
9.70M
    {
802
9.70M
      n_bfd->origin = n_bfd->proxy_handle.file_offset;
803
9.70M
      if (!bfd_set_filename (n_bfd, filename))
804
0
  goto out;
805
9.70M
    }
806
807
9.71M
  n_bfd->arelt_data = new_areldata;
808
809
  /* Copy BFD_COMPRESS, BFD_DECOMPRESS and BFD_COMPRESS_GABI flags.  */
810
9.71M
  n_bfd->flags |= archive->flags & (BFD_COMPRESS
811
9.71M
            | BFD_DECOMPRESS
812
9.71M
            | BFD_COMPRESS_GABI);
813
814
  /* Copy is_linker_input.  */
815
9.71M
  n_bfd->is_linker_input = archive->is_linker_input;
816
817
9.71M
  if (archive->no_element_cache
818
13.9k
      || _bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd))
819
9.71M
    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
9.71M
}
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
9.73M
{
867
9.73M
  if ((bfd_get_format (archive) != bfd_archive)
868
9.73M
      || (archive->direction == write_direction))
869
0
    {
870
0
      bfd_set_error (bfd_error_invalid_operation);
871
0
      return NULL;
872
0
    }
873
874
9.73M
  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
9.73M
  else
878
9.73M
    return BFD_SEND (archive,
879
9.73M
         openr_next_archived_file, (archive, last_file));
880
9.73M
}
881
882
bfd *
883
bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
884
9.68M
{
885
9.68M
  ufile_ptr filestart;
886
887
9.68M
  BFD_ASSERT (!bfd_is_fake_archive (archive));
888
889
9.68M
  if (!last_file)
890
9.67M
    filestart = bfd_ardata (archive)->first_file.file_offset;
891
9.35k
  else
892
9.35k
    {
893
9.35k
      filestart = last_file->proxy_handle.file_offset;
894
9.35k
      if (! bfd_is_thin_archive (archive))
895
9.33k
  {
896
9.33k
    bfd_size_type size = arelt_size (last_file);
897
898
9.33k
    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
9.33k
    filestart += filestart % 2;
903
9.33k
    if (filestart < last_file->proxy_handle.file_offset)
904
181
      {
905
        /* Prevent looping.  See PR19256.  */
906
181
        bfd_set_error (bfd_error_malformed_archive);
907
181
        return NULL;
908
181
      }
909
9.33k
  }
910
9.35k
    }
911
912
9.68M
  return _bfd_get_elt_at_filepos (archive, filestart, NULL);
913
9.68M
}
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
32.0M
{
925
32.0M
  char armag[SARMAG + 1];
926
32.0M
  size_t amt;
927
928
32.0M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
929
930
32.0M
  if (bfd_read (armag, SARMAG, abfd) != SARMAG)
931
193k
    {
932
193k
      if (bfd_get_error () != bfd_error_system_call)
933
121k
  bfd_set_error (bfd_error_wrong_format);
934
193k
      return NULL;
935
193k
    }
936
937
31.8M
  bfd_set_thin_archive (abfd, strncmp (armag, ARMAGT, SARMAG) == 0);
938
939
31.8M
  if (strncmp (armag, ARMAG, SARMAG) != 0
940
20.8M
      && ! bfd_is_thin_archive (abfd))
941
20.7M
    {
942
20.7M
      bfd_set_error (bfd_error_wrong_format);
943
20.7M
      return NULL;
944
20.7M
    }
945
946
11.0M
  amt = sizeof (struct artdata);
947
11.0M
  bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
948
11.0M
  if (bfd_ardata (abfd) == NULL)
949
0
    return NULL;
950
951
11.0M
  bfd_ardata (abfd)->first_file.file_offset = SARMAG;
952
953
11.0M
  if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
954
9.80M
      || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
955
1.35M
    {
956
1.35M
      if (bfd_get_error () != bfd_error_system_call)
957
1.35M
  bfd_set_error (bfd_error_wrong_format);
958
1.35M
      bfd_release (abfd, bfd_ardata (abfd));
959
1.35M
      return NULL;
960
1.35M
    }
961
962
9.71M
  if (abfd->target_defaulted || abfd->is_linker_input)
963
9.71M
    {
964
9.71M
      bfd *first;
965
9.71M
      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
9.71M
      save = abfd->no_element_cache;
978
9.71M
      abfd->no_element_cache = 1;
979
9.71M
      first = bfd_openr_next_archived_file (abfd, NULL);
980
9.71M
      abfd->no_element_cache = save;
981
9.71M
      if (first != NULL)
982
9.70M
  {
983
9.70M
    first->target_defaulted = false;
984
9.70M
    if (abfd->is_linker_input)
985
0
      first->plugin_format = bfd_plugin_no;
986
9.70M
    if (!bfd_check_format (first, bfd_object)
987
53.8k
        || first->xvec != abfd->xvec)
988
9.64M
      bfd_set_error (bfd_error_wrong_object_format);
989
9.70M
    bfd_close (first);
990
9.70M
  }
991
9.71M
    }
992
993
9.71M
  return _bfd_no_cleanup;
994
11.0M
}
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.04M
#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
6.20M
#define BSD_SYMDEF_COUNT_SIZE 4
1093
1094
/* The size of the string count.  */
1095
6.20M
#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.36M
{
1103
2.36M
  struct areltdata *mapdata;
1104
2.36M
  size_t counter;
1105
2.36M
  bfd_byte *raw_armap, *rbase;
1106
2.36M
  struct artdata *ardata = bfd_ardata (abfd);
1107
2.36M
  char *stringbase;
1108
2.36M
  bfd_size_type parsed_size;
1109
2.36M
  size_t amt, string_size;
1110
2.36M
  carsym *set;
1111
1112
2.36M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1113
1114
2.36M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1115
2.36M
  if (mapdata == NULL)
1116
77.9k
    return false;
1117
2.29M
  parsed_size = mapdata->parsed_size;
1118
2.29M
  free (mapdata);
1119
  /* PR 17512: file: 883ff754.  */
1120
  /* PR 17512: file: 0458885f.  */
1121
2.29M
  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.28M
  raw_armap = (bfd_byte *) _bfd_alloc_and_read (abfd, parsed_size, parsed_size);
1128
2.28M
  if (raw_armap == NULL)
1129
99.5k
    return false;
1130
1131
2.18M
  parsed_size -= BSD_SYMDEF_COUNT_SIZE + BSD_STRING_COUNT_SIZE;
1132
2.18M
  amt = H_GET_32 (abfd, raw_armap);
1133
2.18M
  if (amt > parsed_size
1134
1.73M
      || amt % BSD_SYMDEF_SIZE != 0)
1135
462k
    {
1136
      /* Probably we're using the wrong byte ordering.  */
1137
462k
      bfd_set_error (bfd_error_wrong_format);
1138
462k
      goto release_armap;
1139
462k
    }
1140
1141
1.72M
  rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
1142
1.72M
  stringbase = (char *) rbase + amt + BSD_STRING_COUNT_SIZE;
1143
1.72M
  string_size = parsed_size - amt;
1144
1145
1.72M
  ardata->symdef_count = amt / BSD_SYMDEF_SIZE;
1146
1.72M
  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.72M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd, amt);
1152
1.72M
  if (!ardata->symdefs)
1153
0
    goto release_armap;
1154
1155
1.72M
  for (counter = 0, set = ardata->symdefs;
1156
2.31M
       counter < ardata->symdef_count;
1157
1.72M
       counter++, set++, rbase += BSD_SYMDEF_SIZE)
1158
600k
    {
1159
600k
      unsigned nameoff = H_GET_32 (abfd, rbase);
1160
600k
      if (nameoff >= string_size)
1161
10.1k
  {
1162
10.1k
    bfd_set_error (bfd_error_malformed_archive);
1163
10.1k
    goto release_armap;
1164
10.1k
  }
1165
590k
      set->name = stringbase + nameoff;
1166
590k
      set->u.file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1167
590k
    }
1168
1169
1.71M
  ardata->first_file.file_offset = bfd_tell (abfd);
1170
  /* Pad to an even boundary if you have to.  */
1171
1.71M
  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.71M
  abfd->has_armap = true;
1176
1.71M
  return true;
1177
1178
473k
 release_armap:
1179
473k
  ardata->symdef_count = 0;
1180
473k
  ardata->symdefs = NULL;
1181
473k
  bfd_release (abfd, raw_armap);
1182
473k
  return false;
1183
1.72M
}
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.51M
{
1191
1.51M
  struct areltdata *mapdata;
1192
1.51M
  int *raw_armap, *rawptr;
1193
1.51M
  struct artdata *ardata = bfd_ardata (abfd);
1194
1.51M
  char *stringbase;
1195
1.51M
  char *stringend;
1196
1.51M
  bfd_size_type stringsize;
1197
1.51M
  bfd_size_type parsed_size;
1198
1.51M
  ufile_ptr filesize;
1199
1.51M
  size_t nsymz, carsym_size, ptrsize, i;
1200
1.51M
  carsym *carsyms;
1201
1.51M
  bfd_vma (*swap) (const void *);
1202
1.51M
  char int_buf[4];
1203
1.51M
  struct areltdata *tmp;
1204
1205
1.51M
  BFD_ASSERT (!bfd_is_fake_archive (abfd));
1206
1207
1.51M
  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1208
1.51M
  if (mapdata == NULL)
1209
230k
    return false;
1210
1.28M
  parsed_size = mapdata->parsed_size;
1211
1.28M
  free (mapdata);
1212
1213
1.28M
  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.28M
  swap = bfd_getb32;
1219
1.28M
  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.28M
  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.28M
  filesize = bfd_get_file_size (abfd);
1231
1.28M
  ptrsize = 4 * nsymz;
1232
1.28M
  if ((filesize != 0 && parsed_size > filesize)
1233
1.26M
      || parsed_size < 4
1234
1.25M
      || parsed_size - 4 < ptrsize)
1235
40.1k
    {
1236
40.1k
      bfd_set_error (bfd_error_malformed_archive);
1237
40.1k
      return false;
1238
40.1k
    }
1239
1240
1.24M
  stringsize = parsed_size - ptrsize - 4;
1241
1242
1.24M
  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.24M
  raw_armap = (int *) _bfd_malloc_and_read (abfd, ptrsize, ptrsize);
1250
1.24M
  if (raw_armap == NULL)
1251
3.51k
    return false;
1252
1253
1.23M
  ardata->symdefs = (struct carsym *) bfd_alloc (abfd,
1254
1.23M
             carsym_size + stringsize + 1);
1255
1.23M
  if (ardata->symdefs == NULL)
1256
0
    goto free_armap;
1257
1.23M
  carsyms = ardata->symdefs;
1258
1.23M
  stringbase = ((char *) ardata->symdefs) + carsym_size;
1259
1260
1.23M
  if (bfd_read (stringbase, stringsize, abfd) != stringsize)
1261
3.98k
    goto release_symdefs;
1262
1263
  /* OK, build the carsyms.  */
1264
1.23M
  stringend = stringbase + stringsize;
1265
1.23M
  *stringend = 0;
1266
4.85M
  for (i = 0; i < nsymz; i++)
1267
3.62M
    {
1268
3.62M
      rawptr = raw_armap + i;
1269
3.62M
      carsyms->u.file_offset = swap ((bfd_byte *) rawptr);
1270
3.62M
      carsyms->name = stringbase;
1271
3.62M
      stringbase += strlen (stringbase);
1272
3.62M
      if (stringbase != stringend)
1273
3.00M
  ++stringbase;
1274
3.62M
      carsyms++;
1275
3.62M
    }
1276
1277
1.23M
  ardata->symdef_count = nsymz;
1278
1.23M
  ardata->first_file.file_offset = bfd_tell (abfd);
1279
  /* Pad to an even boundary if you have to.  */
1280
1.23M
  ardata->first_file.file_offset += (ardata->first_file.file_offset) % 2;
1281
1.23M
  if (bfd_seek (abfd, ardata->first_file.file_offset, SEEK_SET) != 0)
1282
0
    goto release_symdefs;
1283
1284
1.23M
  abfd->has_armap = true;
1285
1.23M
  free (raw_armap);
1286
1287
  /* Check for a second archive header (as used by PE).  */
1288
1.23M
  tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1289
1.23M
  if (tmp != NULL)
1290
1.22M
    {
1291
1.22M
      if (tmp->arch_header[0] == '/'
1292
136k
    && tmp->arch_header[1] == ' ')
1293
7.56k
  ardata->first_file.file_offset
1294
7.56k
    += (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
1295
1.22M
      free (tmp);
1296
1.22M
    }
1297
1298
1.23M
  return true;
1299
1300
3.98k
 release_symdefs:
1301
3.98k
  bfd_release (abfd, (ardata)->symdefs);
1302
3.98k
 free_armap:
1303
3.98k
  free (raw_armap);
1304
3.98k
  return false;
1305
3.98k
}
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
10.8M
{
1313
10.8M
  char nextname[16];
1314
10.8M
  int i = bfd_read (nextname, 16, abfd);
1315
1316
10.8M
  if (i == 0)
1317
8
    return true;
1318
10.8M
  if (i != 16)
1319
7.13k
    return false;
1320
1321
10.8M
  if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1322
0
    return false;
1323
1324
10.8M
  if (memcmp (nextname, "__.SYMDEF       ", 16) == 0
1325
      /* Old Linux archives.  */
1326
9.52M
      || memcmp (nextname, "__.SYMDEF/      ", 16) == 0)
1327
2.36M
    return do_slurp_bsd_armap (abfd);
1328
8.49M
  else if (memcmp (nextname, "/               ", 16) == 0)
1329
1.51M
    return do_slurp_coff_armap (abfd);
1330
6.97M
  else if (memcmp (nextname, "/SYM64/         ", 16) == 0)
1331
139k
    {
1332
      /* 64bit (Irix 6) archive.  */
1333
139k
#ifdef BFD64
1334
139k
      return _bfd_archive_64_bit_slurp_armap (abfd);
1335
#else
1336
      bfd_set_error (bfd_error_wrong_format);
1337
      return false;
1338
#endif
1339
139k
    }
1340
6.83M
  else if (memcmp (nextname, "________", 8) == 0
1341
360k
     && ((nextname[8] == '_' && nextname[9] == '_')
1342
202k
         || (nextname[8] == '6' && nextname[9] == '4'))
1343
278k
     && nextname[10] == 'E'
1344
211k
     && (nextname[11] == 'B' || nextname[11] == 'L')
1345
202k
     && nextname[12] == 'E'
1346
195k
     && (nextname[13] == 'B' || nextname[13] == 'L')
1347
188k
     && nextname[14] == '_'
1348
183k
     && (nextname[15] == ' ' || nextname[15] == 'X'))
1349
178k
    {
1350
      /* ECOFF archive.  */
1351
178k
      bfd_set_error (bfd_error_wrong_format);
1352
178k
      return false;
1353
178k
    }
1354
6.65M
  else if (memcmp (nextname, "#1/20           ", 16) == 0)
1355
25.6k
    {
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
25.6k
      struct ar_hdr hdr;
1360
25.6k
      char extname[20];
1361
1362
25.6k
      if (bfd_read (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1363
4.37k
  return false;
1364
      /* Read the extended name.  We know its length.  */
1365
21.2k
      if (bfd_read (extname, 20, abfd) != 20)
1366
10.8k
  return false;
1367
10.4k
      if (bfd_seek (abfd, -(file_ptr) (sizeof (hdr) + 20), SEEK_CUR) != 0)
1368
0
  return false;
1369
10.4k
      if (memcmp (extname, "__.SYMDEF SORTED", 16) == 0
1370
9.27k
    || memcmp (extname, "__.SYMDEF", 9) == 0)
1371
4.37k
  return do_slurp_bsd_armap (abfd);
1372
10.4k
    }
1373
1374
6.63M
  abfd->has_armap = false;
1375
6.63M
  return true;
1376
10.8M
}
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
9.80M
{
1393
9.80M
  char nextname[17];
1394
1395
9.80M
  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
9.80M
  if (bfd_seek (abfd, bfd_ardata (abfd)->first_file.file_offset,
1400
9.80M
    SEEK_SET) != 0)
1401
0
    return false;
1402
1403
9.80M
  if (bfd_read (nextname, 16, abfd) == 16)
1404
9.80M
    {
1405
9.80M
      struct areltdata *namedata;
1406
9.80M
      bfd_size_type amt;
1407
9.80M
      ufile_ptr filesize;
1408
1409
9.80M
      if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
1410
0
  return false;
1411
1412
9.80M
      if (! startswith (nextname, "ARFILENAMES/    ")
1413
9.71M
    && ! startswith (nextname, "//              "))
1414
9.52M
  {
1415
9.52M
    bfd_ardata (abfd)->extended_names = NULL;
1416
9.52M
    bfd_ardata (abfd)->extended_names_size = 0;
1417
9.52M
    return true;
1418
9.52M
  }
1419
1420
286k
      namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1421
286k
      if (namedata == NULL)
1422
63.3k
  return false;
1423
1424
223k
      filesize = bfd_get_file_size (abfd);
1425
223k
      amt = namedata->parsed_size;
1426
223k
      if (amt + 1 == 0 || (filesize != 0 && amt > filesize))
1427
24.5k
  {
1428
24.5k
    bfd_set_error (bfd_error_malformed_archive);
1429
24.5k
    goto byebye;
1430
24.5k
  }
1431
1432
198k
      bfd_ardata (abfd)->extended_names_size = amt;
1433
198k
      bfd_ardata (abfd)->extended_names = (char *) bfd_alloc (abfd, amt + 1);
1434
198k
      if (bfd_ardata (abfd)->extended_names == NULL)
1435
0
  {
1436
27.8k
  byebye:
1437
27.8k
    free (namedata);
1438
27.8k
    bfd_ardata (abfd)->extended_names = NULL;
1439
27.8k
    bfd_ardata (abfd)->extended_names_size = 0;
1440
27.8k
    return false;
1441
0
  }
1442
1443
198k
      if (bfd_read (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1444
3.31k
  {
1445
3.31k
    if (bfd_get_error () != bfd_error_system_call)
1446
3.31k
      bfd_set_error (bfd_error_malformed_archive);
1447
3.31k
    bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1448
3.31k
    bfd_ardata (abfd)->extended_names = NULL;
1449
3.31k
    goto byebye;
1450
3.31k
  }
1451
195k
      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
195k
      {
1459
195k
  char *ext_names = bfd_ardata (abfd)->extended_names;
1460
195k
  char *temp = ext_names;
1461
195k
  char *limit = temp + namedata->parsed_size;
1462
1463
8.64M
  for (; temp < limit; ++temp)
1464
8.45M
    {
1465
8.45M
      if (*temp == ARFMAG[1])
1466
320k
        temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1467
8.45M
      if (*temp == '\\')
1468
5.07k
        *temp = '/';
1469
8.45M
    }
1470
195k
  *limit = '\0';
1471
195k
      }
1472
1473
      /* Pad to an even boundary if you have to.  */
1474
195k
      bfd_ardata (abfd)->first_file.file_offset = bfd_tell (abfd);
1475
195k
      bfd_ardata (abfd)->first_file.file_offset +=
1476
195k
  (bfd_ardata (abfd)->first_file.file_offset) % 2;
1477
1478
195k
      free (namedata);
1479
195k
    }
1480
197k
  return true;
1481
9.80M
}
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
999
{
1527
999
  if (abfd->flags & BFD_ARCHIVE_FULL_PATH)
1528
0
    return file;
1529
999
  return lbasename (file);
1530
999
}
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
895
{
1669
895
  *name = "ARFILENAMES/";
1670
895
  return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen);
1671
895
}
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
16.2k
{
1681
16.2k
  *name = "//";
1682
16.2k
  return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen);
1683
16.2k
}
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
33
{
1691
33
  return true;
1692
33
}
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
17.1k
{
1708
17.1k
  unsigned int maxname = ar_maxnamelen (abfd);
1709
17.1k
  bfd_size_type total_namelen = 0;
1710
17.1k
  bfd *current;
1711
17.1k
  char *strptr;
1712
17.1k
  const char *last_filename;
1713
17.1k
  long last_stroff;
1714
1715
17.1k
  *tablen = 0;
1716
17.1k
  last_filename = NULL;
1717
1718
  /* Figure out how long the table should be.  */
1719
17.1k
  for (current = abfd->archive_head;
1720
17.6k
       current != NULL;
1721
17.1k
       current = current->archive_next)
1722
513
    {
1723
513
      const char *normal;
1724
513
      unsigned int thislen;
1725
1726
513
      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
513
      normal = normalize (abfd, bfd_get_filename (current));
1763
513
      if (normal == NULL)
1764
0
  return false;
1765
1766
513
      thislen = strlen (normal);
1767
1768
513
      if (thislen > maxname
1769
2
    && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1770
0
  thislen = maxname;
1771
1772
513
      if (thislen > maxname)
1773
2
  {
1774
    /* Add one to leave room for \n.  */
1775
2
    total_namelen += thislen + 1;
1776
2
    if (trailing_slash)
1777
2
      {
1778
        /* Leave room for trailing slash.  */
1779
2
        ++total_namelen;
1780
2
      }
1781
2
  }
1782
511
      else
1783
511
  {
1784
511
    struct ar_hdr *hdr = arch_hdr (current);
1785
511
    if (filename_ncmp (normal, hdr->ar_name, thislen) != 0
1786
511
        || (thislen < sizeof hdr->ar_name
1787
511
      && hdr->ar_name[thislen] != ar_padchar (current)))
1788
158
      {
1789
        /* Must have been using extended format even though it
1790
     didn't need to.  Fix it to use normal format.  */
1791
158
        memcpy (hdr->ar_name, normal, thislen);
1792
158
        if (thislen < maxname
1793
10
      || (thislen == maxname && thislen < sizeof hdr->ar_name))
1794
158
    hdr->ar_name[thislen] = ar_padchar (current);
1795
158
      }
1796
511
  }
1797
513
    }
1798
1799
17.1k
  if (total_namelen == 0)
1800
17.1k
    return true;
1801
1802
2
  *tabloc = (char *) bfd_alloc (abfd, total_namelen);
1803
2
  if (*tabloc == NULL)
1804
0
    return false;
1805
1806
2
  *tablen = total_namelen;
1807
2
  strptr = *tabloc;
1808
1809
2
  last_filename = NULL;
1810
2
  last_stroff = 0;
1811
1812
2
  for (current = abfd->archive_head;
1813
4
       current != NULL;
1814
2
       current = current->archive_next)
1815
2
    {
1816
2
      const char *normal;
1817
2
      unsigned int thislen;
1818
2
      long stroff;
1819
2
      const char *filename = bfd_get_filename (current);
1820
1821
2
      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
2
      else
1842
2
  {
1843
2
    normal = normalize (abfd, filename);
1844
2
    if (normal == NULL)
1845
0
      return false;
1846
2
  }
1847
1848
2
      thislen = strlen (normal);
1849
2
      if (thislen > maxname || bfd_is_thin_archive (abfd))
1850
2
  {
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
2
    struct ar_hdr *hdr = arch_hdr (current);
1855
2
    if (normal == last_filename)
1856
0
      stroff = last_stroff;
1857
2
    else
1858
2
      {
1859
2
        last_filename = filename;
1860
2
        stroff = strptr - *tabloc;
1861
2
        last_stroff = stroff;
1862
2
        memcpy (strptr, normal, thislen);
1863
2
        strptr += thislen;
1864
2
        if (trailing_slash)
1865
2
    *strptr++ = '/';
1866
2
        *strptr++ = ARFMAG[1];
1867
2
      }
1868
2
    hdr->ar_name[0] = ar_padchar (current);
1869
2
    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
2
    else
1878
2
      _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld", stroff);
1879
2
  }
1880
2
    }
1881
1882
2
  return true;
1883
2
}
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
409
{
1894
409
  unsigned int maxname = ar_maxnamelen (abfd);
1895
409
  bfd *current;
1896
1897
409
  *tablen = 0;
1898
409
  *tabloc = NULL;
1899
409
  *name = NULL;
1900
1901
409
  for (current = abfd->archive_head;
1902
418
       current != NULL;
1903
409
       current = current->archive_next)
1904
9
    {
1905
9
      const char *normal = normalize (abfd, bfd_get_filename (current));
1906
9
      int has_space = 0;
1907
9
      unsigned int len;
1908
1909
9
      if (normal == NULL)
1910
0
  return false;
1911
1912
41
      for (len = 0; normal[len]; len++)
1913
32
  if (normal[len] == ' ')
1914
7
    has_space = 1;
1915
1916
9
      if (len > maxname || has_space)
1917
2
  {
1918
2
    struct ar_hdr *hdr = arch_hdr (current);
1919
1920
2
    len = (len + 3) & ~3;
1921
2
    arch_eltdata (current)->extra_size = len;
1922
2
    _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%lu", len);
1923
2
  }
1924
9
    }
1925
1926
409
  return true;
1927
409
}
1928

1929
/* Write an archive header.  */
1930
1931
bool
1932
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1933
493
{
1934
493
  struct ar_hdr *hdr = arch_hdr (abfd);
1935
1936
493
  if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1937
0
    return false;
1938
493
  return true;
1939
493
}
1940
1941
/* Write an archive header using BSD4.4 convention.  */
1942
1943
bool
1944
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1945
9
{
1946
9
  struct ar_hdr *hdr = arch_hdr (abfd);
1947
1948
9
  if (is_bsd44_extended_name (hdr->ar_name))
1949
2
    {
1950
      /* This is a BSD 4.4 extended name.  */
1951
2
      const char *fullname = normalize (abfd, bfd_get_filename (abfd));
1952
2
      unsigned int len = strlen (fullname);
1953
2
      unsigned int padded_len = (len + 3) & ~3;
1954
1955
2
      BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1956
1957
2
      if (!_bfd_ar_sizepad (hdr->ar_size, sizeof (hdr->ar_size),
1958
2
          arch_eltdata (abfd)->parsed_size + padded_len))
1959
0
  return false;
1960
1961
2
      if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1962
0
  return false;
1963
1964
2
      if (bfd_write (fullname, len, archive) != len)
1965
0
  return false;
1966
1967
2
      if (len & 3)
1968
2
  {
1969
2
    static const char pad[3] = { 0, 0, 0 };
1970
1971
2
    len = 4 - (len & 3);
1972
2
    if (bfd_write (pad, len, archive) != len)
1973
0
      return false;
1974
2
  }
1975
2
    }
1976
7
  else
1977
7
    {
1978
7
      if (bfd_write (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1979
0
  return false;
1980
7
    }
1981
9
  return true;
1982
9
}
1983
1984
bool
1985
_bfd_noarchive_write_ar_hdr (bfd *archive, bfd *abfd ATTRIBUTE_UNUSED)
1986
18
{
1987
18
  return _bfd_bool_bfd_false_error (archive);
1988
18
}
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
548
{
2016
548
  struct stat status;
2017
548
  struct areltdata *ared;
2018
548
  struct ar_hdr *hdr;
2019
548
  size_t amt;
2020
2021
548
  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
548
  else if (stat (filename, &status) != 0)
2032
0
    {
2033
0
      bfd_set_error (bfd_error_system_call);
2034
0
      return NULL;
2035
0
    }
2036
548
  else
2037
548
    {
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
548
      status.st_mtime = bfd_get_current_time (status.st_mtime);
2044
548
    }
2045
2046
  /* If the caller requested that the BFD generate deterministic output,
2047
     fake values for modification time, UID, GID, and file mode.  */
2048
548
  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
548
  amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
2057
548
  ared = (struct areltdata *) bfd_zmalloc (amt);
2058
548
  if (ared == NULL)
2059
0
    return NULL;
2060
548
  hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
2061
2062
  /* ar headers are space padded, not null padded!  */
2063
548
  memset (hdr, ' ', sizeof (struct ar_hdr));
2064
2065
548
  _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
2066
548
        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
548
    _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
2075
548
          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
548
    _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
2084
548
          status.st_gid);
2085
548
  _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
2086
548
        status.st_mode);
2087
548
  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
548
  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
548
  memcpy (hdr->ar_fmag, ARFMAG, 2);
2099
548
  ared->parsed_size = status.st_size;
2100
548
  ared->arch_header = (char *) hdr;
2101
2102
548
  return ared;
2103
548
}
2104
2105
/* Analogous to stat call.  */
2106
2107
int
2108
bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
2109
18.9k
{
2110
18.9k
  struct ar_hdr *hdr;
2111
18.9k
  char *aloser;
2112
2113
18.9k
  if (abfd->arelt_data == NULL)
2114
17.2k
    {
2115
17.2k
      bfd_set_error (bfd_error_invalid_operation);
2116
17.2k
      return -1;
2117
17.2k
    }
2118
2119
1.63k
  hdr = arch_hdr (abfd);
2120
  /* PR 17512: file: 3d9e9fe9.  */
2121
1.63k
  if (hdr == NULL)
2122
0
    return -1;
2123
1.63k
#define foo(arelt, stelt, size)       \
2124
3.42k
  buf->stelt = strtol (hdr->arelt, &aloser, size);  \
2125
3.42k
  if (aloser == hdr->arelt)       \
2126
3.42k
    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.63k
# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
2152
1.63k
#endif
2153
2154
1.63k
  foo (ar_date, st_mtime, 10);
2155
640
  foo2 (ar_uid, st_uid, 10);
2156
578
  foo2 (ar_gid, st_gid, 10);
2157
572
  foo (ar_mode, st_mode, 8);
2158
2159
552
  buf->st_size = arch_eltdata (abfd)->parsed_size;
2160
2161
552
  return 0;
2162
572
}
2163
2164
void
2165
bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2166
473
{
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
473
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2173
473
  size_t length;
2174
473
  const char *filename;
2175
473
  size_t maxlen = ar_maxnamelen (abfd);
2176
2177
473
  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
473
  filename = normalize (abfd, pathname);
2184
473
  if (filename == NULL)
2185
0
    {
2186
      /* FIXME */
2187
0
      abort ();
2188
0
    }
2189
2190
473
  length = strlen (filename);
2191
2192
473
  if (length <= maxlen)
2193
471
    memcpy (hdr->ar_name, filename, length);
2194
2195
  /* Add the padding character if there is room for it.  */
2196
473
  if (length < maxlen
2197
41
      || (length == maxlen && length < sizeof hdr->ar_name))
2198
471
    (hdr->ar_name)[length] = ar_padchar (abfd);
2199
473
}
2200
2201
void
2202
bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
2203
49
{
2204
49
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
2205
49
  size_t length;
2206
49
  const char *filename = lbasename (pathname);
2207
49
  size_t maxlen = ar_maxnamelen (abfd);
2208
2209
49
  length = strlen (filename);
2210
2211
49
  if (length <= maxlen)
2212
49
    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
49
  if (length < maxlen)
2221
45
    (hdr->ar_name)[length] = ar_padchar (abfd);
2222
49
}
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
26
{
2266
26
}
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
17.5k
{
2273
17.5k
  bfd *current;
2274
17.5k
  char *etable = NULL;
2275
17.5k
  bfd_size_type elength = 0;
2276
17.5k
  const char *ename = NULL;
2277
17.5k
  bool makemap = bfd_has_map (arch);
2278
  /* If no .o's, don't bother to make a map.  */
2279
17.5k
  bool hasobjects = false;
2280
17.5k
  bfd_size_type wrote;
2281
17.5k
  int tries;
2282
17.5k
  char *armag;
2283
17.5k
  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
17.5k
  for (current = arch->archive_head;
2289
18.1k
       current != NULL;
2290
17.5k
       current = current->archive_next)
2291
548
    {
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
548
      if (bfd_write_p (current))
2298
0
  {
2299
0
    bfd_set_error (bfd_error_invalid_operation);
2300
0
    goto input_err;
2301
0
  }
2302
548
      if (!current->arelt_data)
2303
548
  {
2304
548
    current->arelt_data =
2305
548
      bfd_ar_hdr_from_filesystem (arch, bfd_get_filename (current),
2306
548
          current);
2307
548
    if (!current->arelt_data)
2308
0
      goto input_err;
2309
2310
    /* Put in the file name.  */
2311
548
    BFD_SEND (arch, _bfd_truncate_arname,
2312
548
        (arch, bfd_get_filename (current),
2313
548
         (char *) arch_hdr (current)));
2314
548
  }
2315
2316
548
      if (makemap && ! hasobjects)
2317
404
  {     /* Don't bother if we won't make a map!  */
2318
404
    if ((bfd_check_format (current, bfd_object)))
2319
227
      hasobjects = true;
2320
404
  }
2321
548
    }
2322
2323
17.5k
  if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
2324
17.5k
     (arch, &etable, &elength, &ename)))
2325
0
    return false;
2326
2327
17.5k
  if (bfd_seek (arch, 0, SEEK_SET) != 0)
2328
0
    return false;
2329
17.5k
  armag = ARMAG;
2330
17.5k
  if (bfd_is_thin_archive (arch))
2331
177
    armag = ARMAGT;
2332
17.5k
  wrote = bfd_write (armag, SARMAG, arch);
2333
17.5k
  if (wrote != SARMAG)
2334
0
    return false;
2335
2336
17.5k
  if (makemap && hasobjects)
2337
227
    {
2338
227
      if (!_bfd_compute_and_push_armap (arch, (unsigned int) elength, false,
2339
227
          _bfd_write_armap))
2340
19
  return false;
2341
227
    }
2342
2343
17.5k
  if (elength != 0)
2344
2
    {
2345
2
      struct ar_hdr hdr;
2346
2347
2
      memset (&hdr, ' ', sizeof (struct ar_hdr));
2348
2
      memcpy (hdr.ar_name, ename, strlen (ename));
2349
      /* Round size up to even number in archive header.  */
2350
2
      if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size),
2351
2
          (elength + 1) & ~(bfd_size_type) 1))
2352
0
  return false;
2353
2
      memcpy (hdr.ar_fmag, ARFMAG, 2);
2354
2
      if ((bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2355
2
     != sizeof (struct ar_hdr))
2356
2
    || bfd_write (etable, elength, arch) != elength)
2357
0
  return false;
2358
2
      if ((elength % 2) == 1)
2359
1
  {
2360
1
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2361
0
      return false;
2362
1
  }
2363
2
    }
2364
2365
18.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
17.5k
  buffer = bfd_malloc (AR_WRITE_BUFFERSIZE);
2370
17.5k
  if (buffer == NULL)
2371
0
    goto input_err;
2372
2373
17.5k
  for (current = arch->archive_head;
2374
18.0k
       current != NULL;
2375
17.5k
       current = current->archive_next)
2376
520
    {
2377
520
      bfd_size_type remaining = arelt_size (current);
2378
2379
      /* Write ar header.  */
2380
520
      if (!_bfd_write_ar_hdr (arch, current))
2381
18
  goto input_err;
2382
502
      if (bfd_is_thin_archive (arch))
2383
0
  continue;
2384
502
      if (bfd_seek (current, 0, SEEK_SET) != 0)
2385
0
  goto input_err;
2386
2387
1.00k
      while (remaining)
2388
500
  {
2389
500
    size_t amt = AR_WRITE_BUFFERSIZE;
2390
2391
500
    if (amt > remaining)
2392
500
      amt = remaining;
2393
500
    errno = 0;
2394
500
    if (bfd_read (buffer, amt, current) != amt)
2395
0
      goto input_err;
2396
500
    if (bfd_write (buffer, amt, arch) != amt)
2397
0
      goto input_err;
2398
500
    remaining -= amt;
2399
500
  }
2400
2401
502
      if ((arelt_size (current) % 2) == 1)
2402
81
  {
2403
81
    if (bfd_write (&ARFMAG[1], 1, arch) != 1)
2404
0
      goto input_err;
2405
81
  }
2406
502
    }
2407
2408
17.5k
  free (buffer);
2409
2410
17.5k
  if (makemap && hasobjects)
2411
208
    {
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
208
      tries = 1;
2419
208
      do
2420
208
  {
2421
208
    if (bfd_update_armap_timestamp (arch))
2422
208
      break;
2423
0
    _bfd_error_handler
2424
0
      (_("warning: writing archive was slow: rewriting timestamp"));
2425
0
  }
2426
208
      while (++tries < 6);
2427
208
    }
2428
2429
17.5k
  return true;
2430
2431
18
 input_err:
2432
18
  bfd_set_input_error (current, bfd_get_error ());
2433
18
  free (buffer);
2434
18
  return false;
2435
17.5k
}
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
208
{
2445
  /* Dunno if this is the best place for this info...  */
2446
208
  if (elength != 0)
2447
0
    elength += sizeof (struct ar_hdr);
2448
208
  elength += elength % 2;
2449
2450
208
  return BFD_SEND (arch, write_armap,
2451
208
       (arch, elength, map, orl_count, stridx));
2452
208
}
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
227
{
2464
227
  char *first_name = NULL;
2465
227
  bfd *current;
2466
227
  struct orl *map = NULL;
2467
227
  unsigned int orl_max = 1024;    /* Fine initial default.  */
2468
227
  unsigned int orl_count = 0;
2469
227
  int stridx = 0;
2470
227
  asymbol **syms = NULL;
2471
227
  long syms_max = 0;
2472
227
  bool ret;
2473
227
  size_t amt;
2474
227
  static bool report_plugin_err = true;
2475
2476
227
  amt = orl_max * sizeof (struct orl);
2477
227
  map = (struct orl *) bfd_malloc (amt);
2478
227
  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
227
  first_name = (char *) bfd_alloc (arch, 1);
2484
227
  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
227
  while (arch->archive_head
2489
227
   && 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
227
  for (current = arch->archive_head;
2494
438
       current != NULL;
2495
227
       current = current->archive_next)
2496
230
    {
2497
230
      if (bfd_check_format (current, bfd_object)
2498
227
    && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
2499
67
  {
2500
67
    long storage;
2501
67
    long symcount;
2502
67
    long src_count;
2503
2504
67
    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
67
    storage = bfd_get_symtab_upper_bound (current);
2515
67
    if (storage < 0)
2516
15
      goto error_return;
2517
2518
52
    if (storage != 0)
2519
52
      {
2520
52
        if (storage > syms_max)
2521
52
    {
2522
52
      free (syms);
2523
52
      syms_max = storage;
2524
52
      syms = (asymbol **) bfd_malloc (syms_max);
2525
52
      if (syms == NULL)
2526
0
        goto error_return;
2527
52
    }
2528
52
        symcount = bfd_canonicalize_symtab (current, syms);
2529
52
        if (symcount < 0)
2530
4
    goto error_return;
2531
2532
        /* Now map over all the symbols, picking out the ones we
2533
     want.  */
2534
2.33k
        for (src_count = 0; src_count < symcount; src_count++)
2535
2.28k
    {
2536
2.28k
      flagword flags = (syms[src_count])->flags;
2537
2.28k
      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.28k
      bool include_in_armap
2544
2.28k
        = ((((flags & (BSF_GLOBAL
2545
2.28k
            | BSF_WEAK
2546
2.28k
            | BSF_INDIRECT
2547
2.28k
            | BSF_GNU_UNIQUE)) != 0
2548
669
       || bfd_is_com_section (sec))
2549
1.61k
      && ! bfd_is_und_section (sec))
2550
676
           || bfd_coff_pe_weak_external_has_real_fallback
2551
676
          (bfd_asymbol_bfd (syms[src_count]), syms[src_count]));
2552
2553
2.28k
      if (include_in_armap)
2554
1.61k
        {
2555
1.61k
          bfd_size_type namelen;
2556
1.61k
          struct orl *new_map;
2557
2558
          /* This symbol will go into the archive header.  */
2559
1.61k
          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.61k
          if (bfd_lto_slim_symbol_p (current,
2571
1.61k
             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.61k
          namelen = strlen (syms[src_count]->name);
2581
1.61k
          amt = sizeof (char *);
2582
1.61k
          map[orl_count].name = (char **) bfd_alloc (arch, amt);
2583
1.61k
          if (map[orl_count].name == NULL)
2584
0
      goto error_return;
2585
1.61k
          *(map[orl_count].name) = (char *) bfd_alloc (arch,
2586
1.61k
                   namelen + 1);
2587
1.61k
          if (*(map[orl_count].name) == NULL)
2588
0
      goto error_return;
2589
1.61k
          strcpy (*(map[orl_count].name), syms[src_count]->name);
2590
1.61k
          map[orl_count].abfd = current;
2591
1.61k
          map[orl_count].namidx = stridx;
2592
2593
1.61k
          stridx += namelen + 1;
2594
1.61k
          ++orl_count;
2595
1.61k
        }
2596
2.28k
    }
2597
48
      }
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
48
    if (!keep_symtab && !bfd_free_cached_info (current))
2602
0
      goto error_return;
2603
48
  }
2604
230
    }
2605
2606
  /* OK, now we have collected all the data, let's push them out.  */
2607
208
  ret = push_armap (arch, elength, map, orl_count, stridx);
2608
2609
208
  free (syms);
2610
208
  free (map);
2611
208
  if (first_name != NULL)
2612
208
    bfd_release (arch, first_name);
2613
2614
208
  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
227
}
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
217
{
2796
217
  struct stat archstat;
2797
217
  struct ar_hdr hdr;
2798
2799
  /* If creating deterministic archives, just leave the timestamp as-is.  */
2800
217
  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
217
  bfd_flush (arch);
2806
217
  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
217
  if (((long) archstat.st_mtime) <= bfd_ardata (arch)->armap_timestamp)
2815
    /* OK by the linker's rules.  */
2816
13
    return true;
2817
2818
204
  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
204
  bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2826
2827
  /* Prepare an ASCII version suitable for writing.  */
2828
204
  memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2829
204
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2830
204
        bfd_ardata (arch)->armap_timestamp);
2831
2832
  /* Write it into the file.  */
2833
204
  bfd_ardata (arch)->armap_datepos = (SARMAG
2834
204
              + offsetof (struct ar_hdr, ar_date[0]));
2835
204
  if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2836
204
      || (bfd_write (hdr.ar_date, sizeof (hdr.ar_date), arch)
2837
204
    != 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
204
  return false;
2847
204
}
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
183
{
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
183
  unsigned int ranlibsize = (symbol_count * 4) + 4;
2872
183
  unsigned int stringsize = stridx;
2873
183
  unsigned int mapsize = stringsize + ranlibsize;
2874
183
  file_ptr archive_member_file_ptr;
2875
183
  file_ptr first_archive_member_file_ptr;
2876
183
  bfd *current = arch->archive_head;
2877
183
  unsigned int count;
2878
183
  struct ar_hdr hdr;
2879
183
  int padit = mapsize & 1;
2880
2881
183
  if (padit)
2882
23
    mapsize++;
2883
2884
  /* Work out where the first object file will go in the archive.  */
2885
183
  first_archive_member_file_ptr = (mapsize
2886
183
           + elength
2887
183
           + sizeof (struct ar_hdr)
2888
183
           + SARMAG);
2889
2890
183
#ifdef BFD64
2891
183
  current = arch->archive_head;
2892
183
  count = 0;
2893
183
  archive_member_file_ptr = first_archive_member_file_ptr;
2894
228
  while (current != NULL && count < symbol_count)
2895
45
    {
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.65k
      while (count < symbol_count && map[count].abfd == current)
2900
1.61k
  {
2901
1.61k
    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.61k
    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.61k
    count++;
2909
1.61k
  }
2910
45
      archive_member_file_ptr += sizeof (struct ar_hdr);
2911
45
      if (! bfd_is_thin_archive (arch))
2912
45
  {
2913
    /* Add size of this archive entry.  */
2914
45
    archive_member_file_ptr += arelt_size (current);
2915
    /* Remember about the even alignment.  */
2916
45
    archive_member_file_ptr += archive_member_file_ptr % 2;
2917
45
  }
2918
45
      current = current->archive_next;
2919
45
    }
2920
183
#endif
2921
2922
183
  memset (&hdr, ' ', sizeof (struct ar_hdr));
2923
183
  hdr.ar_name[0] = '/';
2924
183
  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
2925
0
    return false;
2926
183
  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2927
183
        ((arch->flags & BFD_DETERMINISTIC_OUTPUT) == 0
2928
183
         ? bfd_get_current_time (0) : 0));
2929
  /* This, at least, is what Intel coff sets the values to.  */
2930
183
  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2931
183
  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2932
183
  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2933
183
  memcpy (hdr.ar_fmag, ARFMAG, 2);
2934
2935
  /* Write the ar header for this item and the number of symbols.  */
2936
183
  if (bfd_write (&hdr, sizeof (struct ar_hdr), arch)
2937
183
      != sizeof (struct ar_hdr))
2938
0
    return false;
2939
2940
183
  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
183
  current = arch->archive_head;
2950
183
  count = 0;
2951
183
  archive_member_file_ptr = first_archive_member_file_ptr;
2952
228
  while (current != NULL && count < symbol_count)
2953
45
    {
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.65k
      while (count < symbol_count && map[count].abfd == current)
2958
1.61k
  {
2959
1.61k
    unsigned int offset = (unsigned int) archive_member_file_ptr;
2960
2961
    /* Catch an attempt to grow an archive past its 4Gb limit.  */
2962
1.61k
    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.61k
    if (!bfd_write_bigendian_4byte_int (arch, offset))
2968
0
      return false;
2969
1.61k
    count++;
2970
1.61k
  }
2971
45
      archive_member_file_ptr += sizeof (struct ar_hdr);
2972
45
      if (! bfd_is_thin_archive (arch))
2973
45
  {
2974
    /* Add size of this archive entry.  */
2975
45
    archive_member_file_ptr += arelt_size (current);
2976
    /* Remember about the even alignment.  */
2977
45
    archive_member_file_ptr += archive_member_file_ptr % 2;
2978
45
  }
2979
45
      current = current->archive_next;
2980
45
    }
2981
2982
  /* Now write the strings themselves.  */
2983
1.79k
  for (count = 0; count < symbol_count; count++)
2984
1.61k
    {
2985
1.61k
      size_t len = strlen (*map[count].name) + 1;
2986
2987
1.61k
      if (bfd_write (*map[count].name, len, arch) != len)
2988
0
  return false;
2989
1.61k
    }
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
183
  if (padit)
2994
23
    {
2995
23
      if (bfd_write ("", 1, arch) != 1)
2996
0
  return false;
2997
23
    }
2998
2999
183
  return true;
3000
183
}
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
9.92M
{
3025
9.92M
  if (arch_eltdata (abfd) != NULL)
3026
9.72M
    {
3027
9.72M
      struct areltdata *ared = arch_eltdata (abfd);
3028
9.72M
      htab_t htab = (htab_t) ared->parent_cache;
3029
3030
9.72M
      if (htab)
3031
13.9k
  {
3032
13.9k
    struct ar_cache ent;
3033
13.9k
    void **slot;
3034
3035
13.9k
    ent.ptr = ared->key;
3036
13.9k
    slot = htab_find_slot (htab, &ent, NO_INSERT);
3037
13.9k
    if (slot != NULL)
3038
13.9k
      {
3039
13.9k
        BFD_ASSERT (((struct ar_cache *) *slot)->arbfd == abfd);
3040
13.9k
        htab_clear_slot (htab, slot);
3041
13.9k
      }
3042
13.9k
  }
3043
9.72M
    }
3044
9.92M
}
3045
3046
bool
3047
_bfd_archive_close_and_cleanup (bfd *abfd)
3048
9.92M
{
3049
9.92M
  if (bfd_write_p (abfd) && abfd->format == bfd_archive)
3050
20.6k
    {
3051
20.6k
      bfd *current;
3052
22.9k
      while ((current = abfd->archive_head) != NULL)
3053
2.33k
  {
3054
2.33k
    abfd->archive_head = current->archive_next;
3055
2.33k
    bfd_close_all_done (current);
3056
2.33k
  }
3057
20.6k
    }
3058
9.92M
  if (bfd_read_p (abfd) && abfd->format == bfd_archive)
3059
42.9k
    {
3060
42.9k
      bfd *nbfd;
3061
42.9k
      bfd *next;
3062
42.9k
      htab_t htab;
3063
3064
      /* Close nested archives (if this bfd is a thin archive).  */
3065
42.9k
      for (nbfd = abfd->nested_archives; nbfd; nbfd = next)
3066
10
  {
3067
10
    next = nbfd->archive_next;
3068
10
    bfd_close (nbfd);
3069
10
  }
3070
3071
42.9k
      htab = bfd_ardata (abfd)->cache;
3072
42.9k
      if (htab)
3073
7.11k
  {
3074
7.11k
    htab_traverse_noresize (htab, archive_close_worker, NULL);
3075
7.11k
    htab_delete (htab);
3076
7.11k
    bfd_ardata (abfd)->cache = NULL;
3077
7.11k
  }
3078
3079
      /* Close the archive plugin file descriptor if needed.  */
3080
42.9k
      if (abfd->archive_plugin_fd > 0)
3081
0
  close (abfd->archive_plugin_fd);
3082
42.9k
    }
3083
3084
9.92M
  _bfd_unlink_from_archive_parent (abfd);
3085
3086
9.92M
  if (abfd->is_linker_output)
3087
0
    (*abfd->link.hash->hash_table_free) (abfd);
3088
3089
  return true;
3090
9.92M
}