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