Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/binutils/elfcomm.c
Line
Count
Source
1
/* elfcomm.c -- common code for ELF format file.
2
   Copyright (C) 2010-2026 Free Software Foundation, Inc.
3
4
   Originally developed by Eric Youngdale <eric@andante.jic.com>
5
   Modifications by Nick Clifton <nickc@redhat.com>
6
7
   This file is part of GNU Binutils.
8
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22
   02110-1301, USA.  */
23
24
/* Do not include bfd.h in this file.  Functions in this file are used
25
   by readelf.c and elfedit.c which define BFD64, and by objdump.c
26
   which doesn't.  */
27
28
#include "sysdep.h"
29
#include "libiberty.h"
30
#include "bfd.h"
31
#include "filenames.h"
32
#include "aout/ar.h"
33
#include "elfcomm.h"
34
#include <assert.h>
35
36
extern char *program_name;
37
38
void
39
error (const char *message, ...)
40
2.51M
{
41
2.51M
  va_list args;
42
43
  /* Try to keep error messages in sync with the program's normal output.  */
44
2.51M
  fflush (stdout);
45
46
2.51M
  va_start (args, message);
47
  //, _("%s: Error: "), program_name);
48
  //, message, args);
49
2.51M
  va_end (args);
50
2.51M
}
51
52
void
53
warn (const char *message, ...)
54
4.72M
{
55
4.72M
  va_list args;
56
57
  /* Try to keep warning messages in sync with the program's normal output.  */
58
4.72M
  fflush (stdout);
59
60
4.72M
  va_start (args, message);
61
  //, _("%s: Warning: "), program_name);
62
  //, message, args);
63
4.72M
  va_end (args);
64
4.72M
}
65
66
void
67
inform (const char *message, ...)
68
0
{
69
0
  va_list args;
70
71
  /* Try to keep info messages in sync with the program's normal output.  */
72
0
  fflush (stdout);
73
74
0
  va_start (args, message);
75
  //, _("%s: Info: "), program_name);
76
  //, message, args);
77
0
  va_end (args);
78
0
}
79
80
void (*byte_put) (unsigned char *, uint64_t, unsigned int);
81
82
void
83
byte_put_little_endian (unsigned char *field, uint64_t value, unsigned int size)
84
1.67k
{
85
1.67k
  if (size > sizeof (uint64_t))
86
0
    {
87
0
      error (_("Unhandled data length: %d\n"), size);
88
0
      abort ();
89
0
    }
90
7.61k
  while (size--)
91
5.94k
    {
92
5.94k
      *field++ = value & 0xff;
93
5.94k
      value >>= 8;
94
5.94k
    }
95
1.67k
}
96
97
void
98
byte_put_big_endian (unsigned char *field, uint64_t value, unsigned int size)
99
0
{
100
0
  if (size > sizeof (uint64_t))
101
0
    {
102
0
      error (_("Unhandled data length: %d\n"), size);
103
0
      abort ();
104
0
    }
105
0
  while (size--)
106
0
    {
107
0
      field[size] = value & 0xff;
108
0
      value >>= 8;
109
0
    }
110
0
}
111
112
uint64_t (*byte_get) (const unsigned char *, unsigned int);
113
114
uint64_t
115
byte_get_little_endian (const unsigned char *field, unsigned int size)
116
40.3M
{
117
40.3M
  switch (size)
118
40.3M
    {
119
1.75M
    case 1:
120
1.75M
      return *field;
121
122
1.76M
    case 2:
123
1.76M
      return ((uint64_t) field[0]
124
1.76M
        | ((uint64_t) field[1] << 8));
125
126
114
    case 3:
127
114
      return ((uint64_t) field[0]
128
114
        | ((uint64_t) field[1] << 8)
129
114
        | ((uint64_t) field[2] << 16));
130
131
35.7M
    case 4:
132
35.7M
      return ((uint64_t) field[0]
133
35.7M
        | ((uint64_t) field[1] << 8)
134
35.7M
        | ((uint64_t) field[2] << 16)
135
35.7M
        | ((uint64_t) field[3] << 24));
136
137
0
    case 5:
138
0
      return ((uint64_t) field[0]
139
0
        | ((uint64_t) field[1] << 8)
140
0
        | ((uint64_t) field[2] << 16)
141
0
        | ((uint64_t) field[3] << 24)
142
0
        | ((uint64_t) field[4] << 32));
143
144
158
    case 6:
145
158
      return ((uint64_t) field[0]
146
158
        | ((uint64_t) field[1] << 8)
147
158
        | ((uint64_t) field[2] << 16)
148
158
        | ((uint64_t) field[3] << 24)
149
158
        | ((uint64_t) field[4] << 32)
150
158
        | ((uint64_t) field[5] << 40));
151
152
2
    case 7:
153
2
      return ((uint64_t) field[0]
154
2
        | ((uint64_t) field[1] << 8)
155
2
        | ((uint64_t) field[2] << 16)
156
2
        | ((uint64_t) field[3] << 24)
157
2
        | ((uint64_t) field[4] << 32)
158
2
        | ((uint64_t) field[5] << 40)
159
2
        | ((uint64_t) field[6] << 48));
160
161
1.04M
    case 8:
162
1.04M
      return ((uint64_t) field[0]
163
1.04M
        | ((uint64_t) field[1] << 8)
164
1.04M
        | ((uint64_t) field[2] << 16)
165
1.04M
        | ((uint64_t) field[3] << 24)
166
1.04M
        | ((uint64_t) field[4] << 32)
167
1.04M
        | ((uint64_t) field[5] << 40)
168
1.04M
        | ((uint64_t) field[6] << 48)
169
1.04M
        | ((uint64_t) field[7] << 56));
170
171
0
    default:
172
0
      error (_("Unhandled data length: %d\n"), size);
173
0
      abort ();
174
40.3M
    }
175
40.3M
}
176
177
uint64_t
178
byte_get_big_endian (const unsigned char *field, unsigned int size)
179
57.6k
{
180
57.6k
  switch (size)
181
57.6k
    {
182
375
    case 1:
183
375
      return *field;
184
185
4.03k
    case 2:
186
4.03k
      return ((uint64_t) field[1]
187
4.03k
        | ((uint64_t) field[0] << 8));
188
189
0
    case 3:
190
0
      return ((uint64_t) field[2]
191
0
        | ((uint64_t) field[1] << 8)
192
0
        | ((uint64_t) field[0] << 16));
193
194
50.5k
    case 4:
195
50.5k
      return ((uint64_t) field[3]
196
50.5k
        | ((uint64_t) field[2] << 8)
197
50.5k
        | ((uint64_t) field[1] << 16)
198
50.5k
        | ((uint64_t) field[0] << 24));
199
200
0
    case 5:
201
0
      return ((uint64_t) field[4]
202
0
        | ((uint64_t) field[3] << 8)
203
0
        | ((uint64_t) field[2] << 16)
204
0
        | ((uint64_t) field[1] << 24)
205
0
        | ((uint64_t) field[0] << 32));
206
207
0
    case 6:
208
0
      return ((uint64_t) field[5]
209
0
        | ((uint64_t) field[4] << 8)
210
0
        | ((uint64_t) field[3] << 16)
211
0
        | ((uint64_t) field[2] << 24)
212
0
        | ((uint64_t) field[1] << 32)
213
0
        | ((uint64_t) field[0] << 40));
214
215
0
    case 7:
216
0
      return ((uint64_t) field[6]
217
0
        | ((uint64_t) field[5] << 8)
218
0
        | ((uint64_t) field[4] << 16)
219
0
        | ((uint64_t) field[3] << 24)
220
0
        | ((uint64_t) field[2] << 32)
221
0
        | ((uint64_t) field[1] << 40)
222
0
        | ((uint64_t) field[0] << 48));
223
224
2.69k
    case 8:
225
2.69k
      return ((uint64_t) field[7]
226
2.69k
        | ((uint64_t) field[6] << 8)
227
2.69k
        | ((uint64_t) field[5] << 16)
228
2.69k
        | ((uint64_t) field[4] << 24)
229
2.69k
        | ((uint64_t) field[3] << 32)
230
2.69k
        | ((uint64_t) field[2] << 40)
231
2.69k
        | ((uint64_t) field[1] << 48)
232
2.69k
        | ((uint64_t) field[0] << 56));
233
234
0
    default:
235
0
      error (_("Unhandled data length: %d\n"), size);
236
0
      abort ();
237
57.6k
    }
238
57.6k
}
239
240
uint64_t
241
byte_get_signed (const unsigned char *field, unsigned int size)
242
150k
{
243
150k
  uint64_t x = byte_get (field, size);
244
245
150k
  switch (size)
246
150k
    {
247
291
    case 1:
248
291
      return (x ^ 0x80) - 0x80;
249
124
    case 2:
250
124
      return (x ^ 0x8000) - 0x8000;
251
2
    case 3:
252
2
      return (x ^ 0x800000) - 0x800000;
253
126k
    case 4:
254
126k
      return (x ^ 0x80000000) - 0x80000000;
255
0
    case 5:
256
0
    case 6:
257
0
    case 7:
258
23.0k
    case 8:
259
      /* Reads of 5-, 6-, and 7-byte numbers are the result of
260
         trying to read past the end of a buffer, and will therefore
261
         not have meaningful values, so we don't try to deal with
262
         the sign in these cases.  */
263
23.0k
      return x;
264
0
    default:
265
0
      abort ();
266
150k
    }
267
150k
}
268
269
/* Return the path name for a proxy entry in a thin archive, adjusted
270
   relative to the path name of the thin archive itself if necessary.
271
   Always returns a pointer to malloc'ed memory.  */
272
273
char *
274
adjust_relative_path (const char *file_name, const char *name,
275
          unsigned long name_len)
276
252
{
277
252
  char * member_file_name;
278
252
  const char * base_name = lbasename (file_name);
279
252
  size_t amt;
280
281
  /* This is a proxy entry for a thin archive member.
282
     If the extended name table contains an absolute path
283
     name, or if the archive is in the current directory,
284
     use the path name as given.  Otherwise, we need to
285
     find the member relative to the directory where the
286
     archive is located.  */
287
252
  if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
288
128
    {
289
128
      amt = name_len + 1;
290
128
      if (amt == 0)
291
0
  return NULL;
292
128
      member_file_name = (char *) malloc (amt);
293
128
      if (member_file_name == NULL)
294
0
        {
295
0
          error (_("Out of memory\n"));
296
0
          return NULL;
297
0
        }
298
128
      memcpy (member_file_name, name, name_len);
299
128
      member_file_name[name_len] = '\0';
300
128
    }
301
124
  else
302
124
    {
303
      /* Concatenate the path components of the archive file name
304
         to the relative path name from the extended name table.  */
305
124
      size_t prefix_len = base_name - file_name;
306
307
124
      amt = prefix_len + name_len + 1;
308
      /* PR 17531: file: 2896dc8b
309
   Catch wraparound.  */
310
124
      if (amt < prefix_len || amt < name_len)
311
0
  {
312
0
    error (_("Abnormal length of thin archive member name: %lx\n"),
313
0
     name_len);
314
0
    return NULL;
315
0
  }
316
317
124
      member_file_name = (char *) malloc (amt);
318
124
      if (member_file_name == NULL)
319
0
        {
320
0
          error (_("Out of memory\n"));
321
0
          return NULL;
322
0
        }
323
124
      memcpy (member_file_name, file_name, prefix_len);
324
124
      memcpy (member_file_name + prefix_len, name, name_len);
325
124
      member_file_name[prefix_len + name_len] = '\0';
326
124
    }
327
252
  return member_file_name;
328
252
}
329
330
/* Processes the archive index table and symbol table in ARCH.
331
   Entries in the index table are SIZEOF_AR_INDEX bytes long.
332
   Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
333
   If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
334
    ARCH->sym_size and ARCH->sym_table.
335
   It is the caller's responsibility to free ARCH->index_array and
336
    ARCH->sym_table.
337
   Returns 1 upon success, 0 otherwise.
338
   If failure occurs an error message is printed.  */
339
340
static int
341
process_archive_index_and_symbols (struct archive_info *arch,
342
           unsigned int sizeof_ar_index,
343
           int read_symbols)
344
707
{
345
707
  size_t got;
346
707
  unsigned long size;
347
707
  char fmag_save;
348
349
707
  fmag_save = arch->arhdr.ar_fmag[0];
350
707
  arch->arhdr.ar_fmag[0] = 0;
351
707
  size = strtoul (arch->arhdr.ar_size, NULL, 10);
352
707
  arch->arhdr.ar_fmag[0] = fmag_save;
353
  /* PR 17531: file: 912bd7de.  */
354
707
  if ((signed long) size < 0)
355
25
    {
356
25
      error (_("%s: invalid archive header size: %ld\n"),
357
25
       arch->file_name, size);
358
25
      return 0;
359
25
    }
360
361
682
  size = size + (size & 1);
362
363
682
  arch->next_arhdr_offset += sizeof arch->arhdr + size;
364
365
682
  if (! read_symbols)
366
682
    {
367
682
      if (fseek (arch->file, size, SEEK_CUR) != 0)
368
0
  {
369
0
    error (_("%s: failed to skip archive symbol table\n"),
370
0
     arch->file_name);
371
0
    return 0;
372
0
  }
373
682
    }
374
0
  else
375
0
    {
376
0
      unsigned long i;
377
      /* A buffer used to hold numbers read in from an archive index.
378
   These are always SIZEOF_AR_INDEX bytes long and stored in
379
   big-endian format.  */
380
0
      unsigned char integer_buffer[sizeof arch->index_num];
381
0
      unsigned char * index_buffer;
382
383
0
      assert (sizeof_ar_index <= sizeof integer_buffer);
384
385
      /* Check the size of the archive index.  */
386
0
      if (size < sizeof_ar_index)
387
0
  {
388
0
    error (_("%s: the archive index is empty\n"), arch->file_name);
389
0
    return 0;
390
0
  }
391
392
      /* Read the number of entries in the archive index.  */
393
0
      got = fread (integer_buffer, 1, sizeof_ar_index, arch->file);
394
0
      if (got != sizeof_ar_index)
395
0
  {
396
0
    error (_("%s: failed to read archive index\n"), arch->file_name);
397
0
    return 0;
398
0
  }
399
400
0
      arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index);
401
0
      size -= sizeof_ar_index;
402
403
0
      if (size < arch->index_num * sizeof_ar_index
404
    /* PR 17531: file: 585515d1.  */
405
0
    || size < arch->index_num)
406
0
  {
407
0
    error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
408
0
     arch->file_name, (long) arch->index_num, sizeof_ar_index, size);
409
0
    return 0;
410
0
  }
411
412
      /* Read in the archive index.  */
413
0
      index_buffer = (unsigned char *)
414
0
  malloc (arch->index_num * sizeof_ar_index);
415
0
      if (index_buffer == NULL)
416
0
  {
417
0
    error (_("Out of memory whilst trying to read archive symbol index\n"));
418
0
    return 0;
419
0
  }
420
421
0
      got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file);
422
0
      if (got != arch->index_num)
423
0
  {
424
0
    free (index_buffer);
425
0
    error (_("%s: failed to read archive index\n"), arch->file_name);
426
0
    return 0;
427
0
  }
428
429
0
      size -= arch->index_num * sizeof_ar_index;
430
431
      /* Convert the index numbers into the host's numeric format.  */
432
0
      arch->index_array = (uint64_t *)
433
0
  malloc (arch->index_num * sizeof (*arch->index_array));
434
0
      if (arch->index_array == NULL)
435
0
  {
436
0
    free (index_buffer);
437
0
    error (_("Out of memory whilst trying to convert the archive symbol index\n"));
438
0
    return 0;
439
0
  }
440
441
0
      for (i = 0; i < arch->index_num; i++)
442
0
  arch->index_array[i] =
443
0
    byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)),
444
0
             sizeof_ar_index);
445
0
      free (index_buffer);
446
447
      /* The remaining space in the header is taken up by the symbol table.  */
448
0
      if (size < 1)
449
0
  {
450
0
    error (_("%s: the archive has an index but no symbols\n"),
451
0
     arch->file_name);
452
0
    return 0;
453
0
  }
454
455
0
      arch->sym_table = (char *) malloc (size);
456
0
      if (arch->sym_table == NULL)
457
0
  {
458
0
    error (_("Out of memory whilst trying to read archive index symbol table\n"));
459
0
    return 0;
460
0
  }
461
462
0
      arch->sym_size = size;
463
0
      got = fread (arch->sym_table, 1, size, arch->file);
464
0
      if (got != size)
465
0
  {
466
0
    error (_("%s: failed to read archive index symbol table\n"),
467
0
     arch->file_name);
468
0
    return 0;
469
0
  }
470
0
    }
471
472
  /* Read the next archive header.  */
473
682
  got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
474
682
  if (got != sizeof arch->arhdr && got != 0)
475
7
    {
476
7
      error (_("%s: failed to read archive header following archive index\n"),
477
7
       arch->file_name);
478
7
      return 0;
479
7
    }
480
481
675
  return 1;
482
682
}
483
484
/* Read the symbol table and long-name table from an archive.  */
485
486
int
487
setup_archive (struct archive_info *arch, const char *file_name,
488
         FILE *file, off_t file_size,
489
         int is_thin_archive, int read_symbols)
490
4.69k
{
491
4.69k
  size_t got;
492
493
4.69k
  arch->file_name = strdup (file_name);
494
4.69k
  arch->file = file;
495
4.69k
  arch->index_num = 0;
496
4.69k
  arch->index_array = NULL;
497
4.69k
  arch->sym_table = NULL;
498
4.69k
  arch->sym_size = 0;
499
4.69k
  arch->longnames = NULL;
500
4.69k
  arch->longnames_size = 0;
501
4.69k
  arch->nested_member_origin = 0;
502
4.69k
  arch->is_thin_archive = is_thin_archive;
503
4.69k
  arch->uses_64bit_indices = 0;
504
4.69k
  arch->next_arhdr_offset = SARMAG;
505
506
  /* Read the first archive member header.  */
507
4.69k
  if (fseek (file, SARMAG, SEEK_SET) != 0)
508
0
    {
509
0
      error (_("%s: failed to seek to first archive header\n"), file_name);
510
0
      return 1;
511
0
    }
512
4.69k
  got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
513
4.69k
  if (got != sizeof arch->arhdr)
514
125
    {
515
125
      if (got == 0)
516
115
  return 0;
517
518
10
      error (_("%s: failed to read archive header\n"), file_name);
519
10
      return 1;
520
125
    }
521
522
  /* See if this is the archive symbol table.  */
523
4.56k
  if (startswith (arch->arhdr.ar_name, "/               "))
524
681
    {
525
681
      if (! process_archive_index_and_symbols (arch, 4, read_symbols))
526
14
  return 1;
527
681
    }
528
3.88k
  else if (startswith (arch->arhdr.ar_name, "/SYM64/         "))
529
26
    {
530
26
      arch->uses_64bit_indices = 1;
531
26
      if (! process_archive_index_and_symbols (arch, 8, read_symbols))
532
18
  return 1;
533
26
    }
534
3.86k
  else if (read_symbols)
535
0
    printf (_("%s has no archive index\n"), file_name);
536
537
4.53k
  if (startswith (arch->arhdr.ar_name, "//              "))
538
202
    {
539
      /* This is the archive string table holding long member names.  */
540
202
      char fmag_save = arch->arhdr.ar_fmag[0];
541
202
      arch->arhdr.ar_fmag[0] = 0;
542
202
      arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
543
202
      arch->arhdr.ar_fmag[0] = fmag_save;
544
      /* PR 17531: file: 01068045.  */
545
202
      if (arch->longnames_size < 8)
546
2
  {
547
2
    error (_("%s: long name table is too small, (size = %" PRId64 ")\n"),
548
2
     file_name, arch->longnames_size);
549
2
    return 1;
550
2
  }
551
      /* PR 17531: file: 639d6a26.  */
552
200
      if ((off_t) arch->longnames_size > file_size
553
158
    || (signed long) arch->longnames_size < 0)
554
84
  {
555
84
    error (_("%s: long name table is too big, (size = %#" PRIx64 ")\n"),
556
84
     file_name, arch->longnames_size);
557
84
    return 1;
558
84
  }
559
560
116
      arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
561
562
      /* Plus one to allow for a string terminator.  */
563
116
      arch->longnames = (char *) malloc (arch->longnames_size + 1);
564
116
      if (arch->longnames == NULL)
565
0
  {
566
0
    error (_("Out of memory reading long symbol names in archive\n"));
567
0
    return 1;
568
0
  }
569
570
116
      if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
571
2
  {
572
2
    free (arch->longnames);
573
2
    arch->longnames = NULL;
574
2
    error (_("%s: failed to read long symbol name string table\n"),
575
2
     file_name);
576
2
    return 1;
577
2
  }
578
579
114
      if ((arch->longnames_size & 1) != 0)
580
15
  getc (file);
581
582
114
      arch->longnames[arch->longnames_size] = 0;
583
114
    }
584
585
4.44k
  return 0;
586
4.53k
}
587
588
/* Open and setup a nested archive, if not already open.  */
589
590
int
591
setup_nested_archive (struct archive_info *nested_arch,
592
          const char *member_file_name)
593
219
{
594
219
  FILE * member_file;
595
219
  struct stat statbuf;
596
597
  /* Have we already setup this archive?  */
598
219
  if (nested_arch->file_name != NULL
599
138
      && streq (nested_arch->file_name, member_file_name))
600
48
    return 0;
601
602
  /* Close previous file and discard cached information.  */
603
171
  if (nested_arch->file != NULL)
604
90
    {
605
90
      fclose (nested_arch->file);
606
90
      nested_arch->file = NULL;
607
90
    }
608
171
  release_archive (nested_arch);
609
610
171
  member_file = fopen (member_file_name, "rb");
611
171
  if (member_file == NULL)
612
47
    return 1;
613
124
  if (fstat (fileno (member_file), &statbuf) < 0)
614
0
    return 1;
615
124
  return setup_archive (nested_arch, member_file_name, member_file,
616
124
      statbuf.st_size, 0, 0);
617
124
}
618
619
/* Release the memory used for the archive information.  */
620
621
void
622
release_archive (struct archive_info * arch)
623
9.30k
{
624
9.30k
  free (arch->file_name);
625
9.30k
  free (arch->index_array);
626
9.30k
  free (arch->sym_table);
627
9.30k
  free (arch->longnames);
628
9.30k
  arch->file_name = NULL;
629
9.30k
  arch->index_array = NULL;
630
9.30k
  arch->sym_table = NULL;
631
9.30k
  arch->longnames = NULL;
632
9.30k
}
633
634
/* Get the name of an archive member from the current archive header.
635
   For simple names, this will modify the ar_name field of the current
636
   archive header.  For long names, it will return a pointer to the
637
   longnames table.  For nested archives, it will open the nested archive
638
   and get the name recursively.  NESTED_ARCH is a single-entry cache so
639
   we don't keep rereading the same information from a nested archive.  */
640
641
char *
642
get_archive_member_name (struct archive_info *arch,
643
                         struct archive_info *nested_arch)
644
53.8k
{
645
53.8k
  unsigned long j, k;
646
647
53.8k
  if (arch->arhdr.ar_name[0] == '/')
648
619
    {
649
      /* We have a long name.  */
650
619
      char *endp;
651
619
      char *member_file_name;
652
619
      char *member_name;
653
619
      char fmag_save;
654
655
619
      if (arch->longnames == NULL || arch->longnames_size == 0)
656
8
  {
657
8
    error (_("Archive member uses long names, but no longname table found\n"));
658
8
    return NULL;
659
8
  }
660
661
611
      arch->nested_member_origin = 0;
662
611
      fmag_save = arch->arhdr.ar_fmag[0];
663
611
      arch->arhdr.ar_fmag[0] = 0;
664
611
      k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
665
611
      if (arch->is_thin_archive && endp != NULL && * endp == ':')
666
229
        arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
667
611
      arch->arhdr.ar_fmag[0] = fmag_save;
668
669
611
      if (j > arch->longnames_size)
670
19
  {
671
19
    error (_("Found long name index (%ld) beyond end of long name table\n"),j);
672
19
    return NULL;
673
19
  }
674
4.00k
      while ((j < arch->longnames_size)
675
3.63k
             && (arch->longnames[j] != '\n')
676
3.63k
             && (arch->longnames[j] != '\0'))
677
3.40k
        j++;
678
592
      if (j > 0 && arch->longnames[j-1] == '/')
679
5
        j--;
680
592
      if (j > arch->longnames_size)
681
0
  j = arch->longnames_size;
682
592
      arch->longnames[j] = '\0';
683
684
592
      if (!arch->is_thin_archive || arch->nested_member_origin == 0)
685
371
  return xstrdup (arch->longnames + k);
686
687
      /* PR 17531: file: 2896dc8b.  */
688
221
      if (k >= j)
689
2
  {
690
2
    error (_("Invalid Thin archive member name\n"));
691
2
    return NULL;
692
2
  }
693
694
      /* This is a proxy for a member of a nested archive.
695
         Find the name of the member in that archive.  */
696
219
      member_file_name = adjust_relative_path (arch->file_name,
697
219
                 arch->longnames + k, j - k);
698
219
      if (member_file_name != NULL
699
219
          && setup_nested_archive (nested_arch, member_file_name) == 0)
700
172
  {
701
172
    member_name = get_archive_member_name_at (nested_arch,
702
172
                arch->nested_member_origin,
703
172
                NULL);
704
172
    if (member_name != NULL)
705
0
      {
706
0
        free (member_file_name);
707
0
        return member_name;
708
0
      }
709
172
  }
710
219
      free (member_file_name);
711
712
      /* Last resort: just return the name of the nested archive.  */
713
219
      return xstrdup (arch->longnames + k);
714
219
    }
715
716
  /* We have a normal (short) name.  */
717
883k
  for (j = 0; j < sizeof (arch->arhdr.ar_name); j++)
718
833k
    if (arch->arhdr.ar_name[j] == '/')
719
3.74k
      {
720
3.74k
  arch->arhdr.ar_name[j] = '\0';
721
3.74k
  return xstrdup (arch->arhdr.ar_name);
722
3.74k
      }
723
724
  /* The full ar_name field is used.  Don't rely on ar_date starting
725
     with a zero byte.  */
726
49.4k
  {
727
49.4k
    char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1);
728
49.4k
    memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name));
729
49.4k
    name[sizeof (arch->arhdr.ar_name)] = '\0';
730
49.4k
    return name;
731
53.1k
  }
732
53.1k
}
733
734
/* Get the name of an archive member at a given OFFSET within an archive
735
   ARCH.  */
736
737
char *
738
get_archive_member_name_at (struct archive_info *arch,
739
                            unsigned long offset,
740
          struct archive_info *nested_arch)
741
172
{
742
172
  size_t got;
743
744
172
  if (fseek (arch->file, offset, SEEK_SET) != 0)
745
4
    {
746
4
      error (_("%s: failed to seek to next file name\n"), arch->file_name);
747
4
      return NULL;
748
4
    }
749
168
  got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
750
168
  if (got != sizeof arch->arhdr)
751
144
    {
752
144
      error (_("%s: failed to read archive header\n"), arch->file_name);
753
144
      return NULL;
754
144
    }
755
24
  if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
756
24
    {
757
24
      error (_("%s: did not find a valid archive header\n"),
758
24
       arch->file_name);
759
24
      return NULL;
760
24
    }
761
762
0
  return get_archive_member_name (arch, nested_arch);
763
24
}
764
765
/* Construct a string showing the name of the archive member, qualified
766
   with the name of the containing archive file.  For thin archives, we
767
   use square brackets to denote the indirection.  For nested archives,
768
   we show the qualified name of the external member inside the square
769
   brackets (e.g., "thin.a[normal.a(foo.o)]").  */
770
771
char *
772
make_qualified_name (struct archive_info * arch,
773
         struct archive_info * nested_arch,
774
         const char *member_name)
775
53.7k
{
776
53.7k
  const char * error_name = _("<corrupt>");
777
53.7k
  size_t len;
778
53.7k
  char * name;
779
780
53.7k
  len = strlen (arch->file_name) + strlen (member_name) + 3;
781
53.7k
  if (arch->is_thin_archive
782
336
      && arch->nested_member_origin != 0)
783
303
    {
784
      /* PR 15140: Allow for corrupt thin archives.  */
785
303
      if (nested_arch->file_name)
786
256
  len += strlen (nested_arch->file_name) + 2;
787
47
      else
788
47
  len += strlen (error_name) + 2;
789
303
    }
790
791
53.7k
  name = (char *) malloc (len);
792
53.7k
  if (name == NULL)
793
0
    {
794
0
      error (_("Out of memory\n"));
795
0
      return NULL;
796
0
    }
797
798
53.7k
  if (arch->is_thin_archive
799
336
      && arch->nested_member_origin != 0)
800
303
    {
801
303
      if (nested_arch->file_name)
802
256
  snprintf (name, len, "%s[%s(%s)]", arch->file_name,
803
256
      nested_arch->file_name, member_name);
804
47
      else
805
47
  snprintf (name, len, "%s[%s(%s)]", arch->file_name,
806
47
      error_name, member_name);
807
303
    }
808
53.4k
  else if (arch->is_thin_archive)
809
33
    snprintf (name, len, "%s[%s]", arch->file_name, member_name);
810
53.4k
  else
811
53.4k
    snprintf (name, len, "%s(%s)", arch->file_name, member_name);
812
813
53.7k
  return name;
814
53.7k
}