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