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