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