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