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