gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / binutils / elfcomm.c
CommitLineData
3284fe0c 1/* elfcomm.c -- common code for ELF format file.
b3adc24a 2 Copyright (C) 2010-2020 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"
30#include "filenames.h"
3284fe0c 31#include "aout/ar.h"
3284fe0c 32#include "elfcomm.h"
c2a7d3f5 33#include <assert.h>
3284fe0c 34
81a65eb3
AM
35extern char *program_name;
36
3284fe0c
L
37void
38error (const char *message, ...)
39{
40 va_list args;
41
fafd911d
NC
42 /* Try to keep error messages in sync with the program's normal output. */
43 fflush (stdout);
44
3284fe0c
L
45 va_start (args, message);
46 fprintf (stderr, _("%s: Error: "), program_name);
47 vfprintf (stderr, message, args);
48 va_end (args);
49}
50
51void
52warn (const char *message, ...)
53{
54 va_list args;
55
fafd911d
NC
56 /* Try to keep warning messages in sync with the program's normal output. */
57 fflush (stdout);
3aade688 58
3284fe0c
L
59 va_start (args, message);
60 fprintf (stderr, _("%s: Warning: "), program_name);
61 vfprintf (stderr, message, args);
62 va_end (args);
63}
64
65void (*byte_put) (unsigned char *, elf_vma, int);
66
67void
68byte_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
97void
98byte_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
dda8d76d 132elf_vma (*byte_get) (const unsigned char *, int);
3284fe0c
L
133
134elf_vma
dda8d76d 135byte_get_little_endian (const unsigned char *field, int size)
3284fe0c
L
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
87bc83b3
CC
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);
1a0670f3 172 /* Fall through. */
87bc83b3
CC
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);
1a0670f3 190 /* Fall through. */
87bc83b3
CC
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);
1a0670f3 209 /* Fall through. */
87bc83b3 210
3284fe0c
L
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);
1a0670f3 229 /* Fall through. */
3284fe0c
L
230
231 default:
232 error (_("Unhandled data length: %d\n"), size);
233 abort ();
234 }
235}
236
237elf_vma
dda8d76d 238byte_get_big_endian (const unsigned char *field, int size)
3284fe0c
L
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
87bc83b3
CC
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 }
1a0670f3 276 /* Fall through. */
87bc83b3
CC
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 }
1a0670f3 296 /* Fall through. */
87bc83b3
CC
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 }
1a0670f3 317 /* Fall through. */
87bc83b3 318
3284fe0c
L
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 {
87bc83b3 331 /* Although we are extracting data from an 8 byte wide field,
3284fe0c
L
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 }
1a0670f3 339 /* Fall through. */
3284fe0c
L
340
341 default:
342 error (_("Unhandled data length: %d\n"), size);
343 abort ();
344 }
345}
346
347elf_vma
dda8d76d 348byte_get_signed (const unsigned char *field, int size)
3284fe0c
L
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;
87bc83b3
CC
358 case 3:
359 return (x ^ 0x800000) - 0x800000;
3284fe0c
L
360 case 4:
361 return (x ^ 0x80000000) - 0x80000000;
87bc83b3
CC
362 case 5:
363 case 6:
364 case 7:
3284fe0c 365 case 8:
87bc83b3
CC
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. */
3284fe0c
L
370 return x;
371 default:
372 abort ();
373 }
374}
375
74bc6052
CC
376/* Return the high-order 32-bits and the low-order 32-bits
377 of an 8-byte value separately. */
378
379void
dda8d76d 380byte_get_64 (const unsigned char *field, elf_vma *high, elf_vma *low)
74bc6052
CC
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
3284fe0c
L
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
399char *
400adjust_relative_path (const char *file_name, const char *name,
591f7597 401 unsigned long name_len)
3284fe0c
L
402{
403 char * member_file_name;
404 const char * base_name = lbasename (file_name);
591f7597 405 size_t amt;
3284fe0c
L
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 {
591f7597
NC
415 amt = name_len + 1;
416 if (amt == 0)
417 return NULL;
418 member_file_name = (char *) malloc (amt);
3284fe0c
L
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;
591f7597
NC
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 }
3aade688 442
591f7597 443 member_file_name = (char *) malloc (amt);
3284fe0c
L
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
c2a7d3f5
NC
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.
81a65eb3 463 Returns 1 upon success, 0 otherwise.
c2a7d3f5
NC
464 If failure occurs an error message is printed. */
465
81a65eb3
AM
466static int
467process_archive_index_and_symbols (struct archive_info *arch,
468 unsigned int sizeof_ar_index,
469 int read_symbols)
c2a7d3f5
NC
470{
471 size_t got;
472 unsigned long size;
d91f0b20 473 char fmag_save;
c2a7d3f5 474
d91f0b20
AM
475 fmag_save = arch->arhdr.ar_fmag[0];
476 arch->arhdr.ar_fmag[0] = 0;
c2a7d3f5 477 size = strtoul (arch->arhdr.ar_size, NULL, 10);
d91f0b20 478 arch->arhdr.ar_fmag[0] = fmag_save;
591f7597
NC
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);
81a65eb3 484 return 0;
591f7597
NC
485 }
486
c2a7d3f5
NC
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);
81a65eb3 497 return 0;
c2a7d3f5
NC
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);
3aade688 510
c2a7d3f5
NC
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);
81a65eb3 515 return 0;
c2a7d3f5
NC
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);
81a65eb3 523 return 0;
c2a7d3f5
NC
524 }
525
526 arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index);
527 size -= sizeof_ar_index;
528
53774b7e
NC
529 if (size < arch->index_num * sizeof_ar_index
530 /* PR 17531: file: 585515d1. */
531 || size < arch->index_num)
c2a7d3f5 532 {
53774b7e 533 error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
c2a7d3f5 534 arch->file_name, (long) arch->index_num, sizeof_ar_index, size);
81a65eb3 535 return 0;
c2a7d3f5
NC
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"));
81a65eb3 544 return 0;
c2a7d3f5
NC
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);
81a65eb3 552 return 0;
c2a7d3f5
NC
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"));
81a65eb3 564 return 0;
c2a7d3f5
NC
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);
81a65eb3 578 return 0;
c2a7d3f5
NC
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"));
81a65eb3 585 return 0;
c2a7d3f5
NC
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);
81a65eb3 594 return 0;
c2a7d3f5
NC
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);
81a65eb3 604 return 0;
c2a7d3f5
NC
605 }
606
81a65eb3 607 return 1;
c2a7d3f5
NC
608}
609
3284fe0c
L
610/* Read the symbol table and long-name table from an archive. */
611
612int
613setup_archive (struct archive_info *arch, const char *file_name,
645ba681 614 FILE *file, off_t file_size,
81a65eb3 615 int is_thin_archive, int read_symbols)
3284fe0c
L
616{
617 size_t got;
3284fe0c
L
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;
81a65eb3 629 arch->uses_64bit_indices = 0;
3284fe0c
L
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. */
c2a7d3f5 649 if (const_strneq (arch->arhdr.ar_name, "/ "))
3284fe0c 650 {
c2a7d3f5
NC
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 {
81a65eb3 656 arch->uses_64bit_indices = 1;
c2a7d3f5
NC
657 if (! process_archive_index_and_symbols (arch, 8, read_symbols))
658 return 1;
3284fe0c
L
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. */
d91f0b20
AM
666 char fmag_save = arch->arhdr.ar_fmag[0];
667 arch->arhdr.ar_fmag[0] = 0;
3284fe0c 668 arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
d91f0b20 669 arch->arhdr.ar_fmag[0] = fmag_save;
591f7597
NC
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 }
058037d3 677 /* PR 17531: file: 639d6a26. */
645ba681 678 if ((off_t) arch->longnames_size > file_size
780f96ae 679 || (signed long) arch->longnames_size < 0)
058037d3
NC
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
3284fe0c
L
686 arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
687
591f7597
NC
688 /* Plus one to allow for a string terminator. */
689 arch->longnames = (char *) malloc (arch->longnames_size + 1);
3284fe0c
L
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);
058037d3
NC
707
708 arch->longnames[arch->longnames_size] = 0;
3284fe0c
L
709 }
710
711 return 0;
712}
713
714/* Open and setup a nested archive, if not already open. */
715
716int
717setup_nested_archive (struct archive_info *nested_arch,
718 const char *member_file_name)
719{
720 FILE * member_file;
780f96ae 721 struct stat statbuf;
3284fe0c
L
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;
780f96ae
AM
736 if (fstat (fileno (member_file), &statbuf) < 0)
737 return 1;
3284fe0c 738 return setup_archive (nested_arch, member_file_name, member_file,
81a65eb3 739 statbuf.st_size, 0, 0);
3284fe0c
L
740}
741
742/* Release the memory used for the archive information. */
743
744void
745release_archive (struct archive_info * arch)
746{
9db70fc3
AM
747 free (arch->file_name);
748 free (arch->index_array);
749 free (arch->sym_table);
750 free (arch->longnames);
3284fe0c
L
751}
752
753/* Get the name of an archive member from the current archive header.
754 For simple names, this will modify the ar_name field of the current
755 archive header. For long names, it will return a pointer to the
756 longnames table. For nested archives, it will open the nested archive
757 and get the name recursively. NESTED_ARCH is a single-entry cache so
758 we don't keep rereading the same information from a nested archive. */
759
760char *
761get_archive_member_name (struct archive_info *arch,
762 struct archive_info *nested_arch)
763{
764 unsigned long j, k;
765
766 if (arch->arhdr.ar_name[0] == '/')
767 {
768 /* We have a long name. */
769 char *endp;
770 char *member_file_name;
771 char *member_name;
d91f0b20 772 char fmag_save;
3284fe0c 773
907b01b7
NC
774 if (arch->longnames == NULL || arch->longnames_size == 0)
775 {
776 error (_("Archive member uses long names, but no longname table found\n"));
777 return NULL;
778 }
3aade688 779
3284fe0c 780 arch->nested_member_origin = 0;
d91f0b20
AM
781 fmag_save = arch->arhdr.ar_fmag[0];
782 arch->arhdr.ar_fmag[0] = 0;
3284fe0c
L
783 k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
784 if (arch->is_thin_archive && endp != NULL && * endp == ':')
785 arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
d91f0b20 786 arch->arhdr.ar_fmag[0] = fmag_save;
3284fe0c 787
591f7597
NC
788 if (j > arch->longnames_size)
789 {
790 error (_("Found long name index (%ld) beyond end of long name table\n"),j);
791 return NULL;
792 }
3284fe0c
L
793 while ((j < arch->longnames_size)
794 && (arch->longnames[j] != '\n')
795 && (arch->longnames[j] != '\0'))
796 j++;
591f7597 797 if (j > 0 && arch->longnames[j-1] == '/')
3284fe0c 798 j--;
591f7597
NC
799 if (j > arch->longnames_size)
800 j = arch->longnames_size;
3284fe0c
L
801 arch->longnames[j] = '\0';
802
803 if (!arch->is_thin_archive || arch->nested_member_origin == 0)
fd486f32 804 return xstrdup (arch->longnames + k);
3284fe0c 805
591f7597
NC
806 /* PR 17531: file: 2896dc8b. */
807 if (k >= j)
808 {
809 error (_("Invalid Thin archive member name\n"));
810 return NULL;
811 }
3aade688 812
3284fe0c
L
813 /* This is a proxy for a member of a nested archive.
814 Find the name of the member in that archive. */
815 member_file_name = adjust_relative_path (arch->file_name,
816 arch->longnames + k, j - k);
817 if (member_file_name != NULL
818 && setup_nested_archive (nested_arch, member_file_name) == 0)
819 {
fd486f32 820 member_name = get_archive_member_name_at (nested_arch,
3284fe0c
L
821 arch->nested_member_origin,
822 NULL);
823 if (member_name != NULL)
824 {
825 free (member_file_name);
826 return member_name;
827 }
828 }
829 free (member_file_name);
830
831 /* Last resort: just return the name of the nested archive. */
fd486f32 832 return xstrdup (arch->longnames + k);
3284fe0c
L
833 }
834
835 /* We have a normal (short) name. */
836 for (j = 0; j < sizeof (arch->arhdr.ar_name); j++)
837 if (arch->arhdr.ar_name[j] == '/')
838 {
839 arch->arhdr.ar_name[j] = '\0';
fd486f32 840 return xstrdup (arch->arhdr.ar_name);
3284fe0c
L
841 }
842
843 /* The full ar_name field is used. Don't rely on ar_date starting
844 with a zero byte. */
845 {
846 char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1);
847 memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name));
848 name[sizeof (arch->arhdr.ar_name)] = '\0';
849 return name;
850 }
851}
852
853/* Get the name of an archive member at a given OFFSET within an archive
854 ARCH. */
855
856char *
857get_archive_member_name_at (struct archive_info *arch,
858 unsigned long offset,
859 struct archive_info *nested_arch)
860{
861 size_t got;
862
863 if (fseek (arch->file, offset, SEEK_SET) != 0)
864 {
865 error (_("%s: failed to seek to next file name\n"), arch->file_name);
866 return NULL;
867 }
868 got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
869 if (got != sizeof arch->arhdr)
870 {
871 error (_("%s: failed to read archive header\n"), arch->file_name);
872 return NULL;
873 }
874 if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
875 {
876 error (_("%s: did not find a valid archive header\n"),
877 arch->file_name);
878 return NULL;
879 }
880
881 return get_archive_member_name (arch, nested_arch);
882}
883
884/* Construct a string showing the name of the archive member, qualified
885 with the name of the containing archive file. For thin archives, we
886 use square brackets to denote the indirection. For nested archives,
887 we show the qualified name of the external member inside the square
888 brackets (e.g., "thin.a[normal.a(foo.o)]"). */
889
890char *
891make_qualified_name (struct archive_info * arch,
892 struct archive_info * nested_arch,
893 const char *member_name)
894{
a043396b 895 const char * error_name = _("<corrupt>");
3284fe0c
L
896 size_t len;
897 char * name;
898
899 len = strlen (arch->file_name) + strlen (member_name) + 3;
a043396b
NC
900 if (arch->is_thin_archive
901 && arch->nested_member_origin != 0)
902 {
903 /* PR 15140: Allow for corrupt thin archives. */
904 if (nested_arch->file_name)
905 len += strlen (nested_arch->file_name) + 2;
906 else
907 len += strlen (error_name) + 2;
908 }
3284fe0c
L
909
910 name = (char *) malloc (len);
911 if (name == NULL)
912 {
913 error (_("Out of memory\n"));
914 return NULL;
915 }
916
a043396b
NC
917 if (arch->is_thin_archive
918 && arch->nested_member_origin != 0)
919 {
920 if (nested_arch->file_name)
921 snprintf (name, len, "%s[%s(%s)]", arch->file_name,
922 nested_arch->file_name, member_name);
923 else
924 snprintf (name, len, "%s[%s(%s)]", arch->file_name,
3aade688 925 error_name, member_name);
a043396b 926 }
3284fe0c
L
927 else if (arch->is_thin_archive)
928 snprintf (name, len, "%s[%s]", arch->file_name, member_name);
929 else
930 snprintf (name, len, "%s(%s)", arch->file_name, member_name);
931
932 return name;
933}
This page took 0.445481 seconds and 4 git commands to generate.