Coverage Report

Created: 2025-07-08 11:15

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