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