Coverage Report

Created: 2023-08-28 06:31

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