1 /* ar.c - Archive modify and extract. 2 Copyright (C) 1991-2025 Free Software Foundation, Inc. 3 4 This file is part of GNU Binutils. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 /* 23 Bugs: GNU ar used to check file against filesystem in quick_update and 24 replace operations (would check mtime). Doesn't warn when name truncated. 25 No way to specify pos_end. Error messages should be more consistent. */ 26 27 #include "sysdep.h" 28 #include "bfd.h" 29 #include "libiberty.h" 30 #include "getopt.h" 31 #include "aout/ar.h" 32 #include "bucomm.h" 33 #include "arsup.h" 34 #include "filenames.h" 35 #include "binemul.h" 36 #include "plugin-api.h" 37 #include "plugin.h" 38 #include "ansidecl.h" 39 40 #ifdef __GO32___ 41 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */ 42 #else 43 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */ 44 #endif 45 46 /* Static declarations. */ 47 48 static void mri_emul (void); 49 static const char *normalize (const char *, bfd *); 50 static void remove_output (void); 51 static void map_over_members (bfd *, void (*)(bfd *), char **, int); 52 static void print_contents (bfd * member); 53 static void delete_members (bfd *, char **files_to_delete); 54 55 static void move_members (bfd *, char **files_to_move); 56 static void replace_members 57 (bfd *, char **files_to_replace, bool quick); 58 static void print_descr (bfd * abfd); 59 static void write_archive (bfd *); 60 static int ranlib_only (const char *archname); 61 static int ranlib_touch (const char *archname); 62 static void usage (int); 63 64 /** Globals and flags. */ 66 67 static int mri_mode; 68 69 /* This flag distinguishes between ar and ranlib: 70 1 means this is 'ranlib'; 0 means this is 'ar'. 71 -1 means if we should use argv[0] to decide. */ 72 #ifndef is_ranlib 73 extern int is_ranlib; 74 #endif 75 76 /* Nonzero means don't warn about creating the archive file if necessary. */ 77 int silent_create = 0; 78 79 /* Nonzero means describe each action performed. */ 80 int verbose = 0; 81 82 /* Nonzero means display offsets of files in the archive. */ 83 int display_offsets = 0; 84 85 /* Nonzero means preserve dates of members when extracting them. */ 86 int preserve_dates = 0; 87 88 /* Nonzero means don't replace existing members whose dates are more recent 89 than the corresponding files. */ 90 int newer_only = 0; 91 92 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF 93 member). -1 means we've been explicitly asked to not write a symbol table; 94 +1 means we've been explicitly asked to write it; 95 0 is the default. 96 Traditionally, the default in BSD has been to not write the table. 97 However, for POSIX.2 compliance the default is now to write a symbol table 98 if any of the members are object files. */ 99 int write_armap = 0; 100 101 /* Operate in deterministic mode: write zero for timestamps, uids, 102 and gids for archive members and the archive symbol table, and write 103 consistent file modes. */ 104 int deterministic = -1; /* Determinism indeterminate. */ 105 106 /* Nonzero means it's the name of an existing member; position new or moved 107 files with respect to this one. */ 108 char *posname = NULL; 109 110 /* Sez how to use `posname': pos_before means position before that member. 111 pos_after means position after that member. pos_end means always at end. 112 pos_default means default appropriately. For the latter two, `posname' 113 should also be zero. */ 114 enum pos 115 { 116 pos_default, pos_before, pos_after, pos_end 117 } postype = pos_default; 118 119 enum operations 120 { 121 none = 0, del, replace, print_table, 122 print_files, extract, move, quick_append 123 } operation = none; 124 125 static bfd ** 126 get_pos_bfd (bfd **, enum pos, const char *); 127 128 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only 129 extract the COUNTED_NAME_COUNTER instance of that name. */ 130 static bool counted_name_mode = 0; 131 static int counted_name_counter = 0; 132 133 /* Whether to truncate names of files stored in the archive. */ 134 static bool ar_truncate = false; 135 136 /* Whether to use a full file name match when searching an archive. 137 This is convenient for archives created by the Microsoft lib 138 program. */ 139 static bool full_pathname = false; 140 141 /* Whether to create a "thin" archive (symbol index only -- no files). */ 142 static bool make_thin_archive = false; 143 144 #define LIBDEPS "__.LIBDEP" 145 /* Text to store in the __.LIBDEP archive element for the linker to use. */ 146 static char * libdeps = NULL; 147 static bfd * libdeps_bfd = NULL; 148 149 static int show_version = 0; 150 151 static int show_help = 0; 152 153 #if BFD_SUPPORTS_PLUGINS 154 static const char *plugin_target = "plugin"; 155 #else 156 static const char *plugin_target = NULL; 157 #endif 158 159 static const char *target = NULL; 160 161 enum long_option_numbers 162 { 163 OPTION_PLUGIN = 201, 164 OPTION_TARGET, 165 OPTION_OUTPUT 166 }; 167 168 static const char * output_dir = NULL; 169 170 static struct option long_options[] = 171 { 172 {"help", no_argument, &show_help, 1}, 173 {"plugin", required_argument, NULL, OPTION_PLUGIN}, 174 {"target", required_argument, NULL, OPTION_TARGET}, 175 {"version", no_argument, &show_version, 1}, 176 {"output", required_argument, NULL, OPTION_OUTPUT}, 177 {"record-libdeps", required_argument, NULL, 'l'}, 178 {"thin", no_argument, NULL, 'T'}, 179 {NULL, no_argument, NULL, 0} 180 }; 181 182 int interactive = 0; 183 184 static void 185 mri_emul (void) 186 { 187 interactive = isatty (fileno (stdin)); 188 yyparse (); 189 } 190 191 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero, 192 COUNT is the length of the FILES chain; FUNCTION is called on each entry 193 whose name matches one in FILES. */ 194 195 static void 196 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count) 197 { 198 bfd *head; 199 int match_count; 200 201 if (count == 0) 202 { 203 for (head = arch->archive_next; head; head = head->archive_next) 204 function (head); 205 return; 206 } 207 208 /* This may appear to be a baroque way of accomplishing what we want. 209 However we have to iterate over the filenames in order to notice where 210 a filename is requested but does not exist in the archive. Ditto 211 mapping over each file each time -- we want to hack multiple 212 references. */ 213 214 for (head = arch->archive_next; head; head = head->archive_next) 215 head->archive_pass = 0; 216 217 for (; count > 0; files++, count--) 218 { 219 bool found = false; 220 221 match_count = 0; 222 for (head = arch->archive_next; head; head = head->archive_next) 223 { 224 const char * filename; 225 226 /* PR binutils/15796: Once an archive element has been matched 227 do not match it again. If the user provides multiple same-named 228 parameters on the command line their intent is to match multiple 229 same-named entries in the archive, not the same entry multiple 230 times. */ 231 if (head->archive_pass) 232 continue; 233 234 filename = bfd_get_filename (head); 235 if (filename == NULL) 236 { 237 /* Some archive formats don't get the filenames filled in 238 until the elements are opened. */ 239 struct stat buf; 240 bfd_stat_arch_elt (head, &buf); 241 } 242 else if (bfd_is_thin_archive (arch)) 243 { 244 /* Thin archives store full pathnames. Need to normalize. */ 245 filename = normalize (filename, arch); 246 } 247 248 if (filename != NULL 249 && !FILENAME_CMP (normalize (*files, arch), filename)) 250 { 251 ++match_count; 252 if (counted_name_mode 253 && match_count != counted_name_counter) 254 { 255 /* Counting, and didn't match on count; go on to the 256 next one. */ 257 continue; 258 } 259 260 found = true; 261 function (head); 262 head->archive_pass = 1; 263 /* PR binutils/15796: Once a file has been matched, do not 264 match any more same-named files in the archive. If the 265 user does want to match multiple same-name files in an 266 archive they should provide multiple same-name parameters 267 to the ar command. */ 268 break; 269 } 270 } 271 272 if (!found) 273 /* xgettext:c-format */ 274 fprintf (stderr, _("no entry %s in archive\n"), *files); 275 } 276 } 277 278 bool operation_alters_arch = false; 280 281 static void 282 usage (int help) 283 { 284 FILE *s; 285 286 #if BFD_SUPPORTS_PLUGINS 287 /* xgettext:c-format */ 288 const char *command_line 289 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]" 290 " [--plugin <name>] [member-name] [count] archive-file file...\n"); 291 292 #else 293 /* xgettext:c-format */ 294 const char *command_line 295 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]" 296 " [member-name] [count] archive-file file...\n"); 297 #endif 298 s = help ? stdout : stderr; 299 300 fprintf (s, command_line, program_name); 301 302 /* xgettext:c-format */ 303 fprintf (s, _(" %s -M [<mri-script]\n"), program_name); 304 fprintf (s, _(" commands:\n")); 305 fprintf (s, _(" d - delete file(s) from the archive\n")); 306 fprintf (s, _(" m[ab] - move file(s) in the archive\n")); 307 fprintf (s, _(" p - print file(s) found in the archive\n")); 308 fprintf (s, _(" q[f] - quick append file(s) to the archive\n")); 309 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n")); 310 fprintf (s, _(" s - act as ranlib\n")); 311 fprintf (s, _(" t[O][v] - display contents of the archive\n")); 312 fprintf (s, _(" x[o] - extract file(s) from the archive\n")); 313 fprintf (s, _(" command specific modifiers:\n")); 314 fprintf (s, _(" [a] - put file(s) after [member-name]\n")); 315 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n")); 316 if (DEFAULT_AR_DETERMINISTIC) 317 { 318 fprintf (s, _("\ 319 [D] - use zero for timestamps and uids/gids (default)\n")); 320 fprintf (s, _("\ 321 [U] - use actual timestamps and uids/gids\n")); 322 } 323 else 324 { 325 fprintf (s, _("\ 326 [D] - use zero for timestamps and uids/gids\n")); 327 fprintf (s, _("\ 328 [U] - use actual timestamps and uids/gids (default)\n")); 329 } 330 fprintf (s, _(" [N] - use instance [count] of name\n")); 331 fprintf (s, _(" [f] - truncate inserted file names\n")); 332 fprintf (s, _(" [P] - use full path names when matching\n")); 333 fprintf (s, _(" [o] - preserve original dates\n")); 334 fprintf (s, _(" [O] - display offsets of files in the archive\n")); 335 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n")); 336 fprintf (s, _(" generic modifiers:\n")); 337 fprintf (s, _(" [c] - do not warn if the library had to be created\n")); 338 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n")); 339 fprintf (s, _(" [l <text> ] - specify the dependencies of this library\n")); 340 fprintf (s, _(" [S] - do not build a symbol table\n")); 341 fprintf (s, _(" [T] - deprecated, use --thin instead\n")); 342 fprintf (s, _(" [v] - be verbose\n")); 343 fprintf (s, _(" [V] - display the version number\n")); 344 fprintf (s, _(" @<file> - read options from <file>\n")); 345 fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n")); 346 fprintf (s, _(" --output=DIRNAME - specify the output directory for extraction operations\n")); 347 fprintf (s, _(" --record-libdeps=<text> - specify the dependencies of this library\n")); 348 fprintf (s, _(" --thin - make a thin archive\n")); 349 #if BFD_SUPPORTS_PLUGINS 350 fprintf (s, _(" optional:\n")); 351 fprintf (s, _(" --plugin <p> - load the specified plugin\n")); 352 #endif 353 354 ar_emul_usage (s); 355 356 list_supported_targets (program_name, s); 357 358 if (REPORT_BUGS_TO[0] && help) 359 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO); 360 361 xexit (help ? 0 : 1); 362 } 363 364 static void 365 ranlib_usage (int help) 366 { 367 FILE *s; 368 369 s = help ? stdout : stderr; 370 371 /* xgettext:c-format */ 372 fprintf (s, _("Usage: %s [options] archive\n"), program_name); 373 fprintf (s, _(" Generate an index to speed access to archives\n")); 374 fprintf (s, _(" The options are:\n\ 375 @<file> Read options from <file>\n")); 376 #if BFD_SUPPORTS_PLUGINS 377 fprintf (s, _("\ 378 --plugin <name> Load the specified plugin\n")); 379 #endif 380 if (DEFAULT_AR_DETERMINISTIC) 381 fprintf (s, _("\ 382 -D Use zero for symbol map timestamp (default)\n\ 383 -U Use an actual symbol map timestamp\n")); 384 else 385 fprintf (s, _("\ 386 -D Use zero for symbol map timestamp\n\ 387 -U Use actual symbol map timestamp (default)\n")); 388 fprintf (s, _("\ 389 -t Update the archive's symbol map timestamp\n\ 390 -h --help Print this help message\n\ 391 -v --version Print version information\n")); 392 393 list_supported_targets (program_name, s); 394 395 if (REPORT_BUGS_TO[0] && help) 396 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO); 397 398 xexit (help ? 0 : 1); 399 } 400 401 /* Normalize a file name specified on the command line into a file 402 name which we will use in an archive. */ 403 404 static const char * 405 normalize (const char *file, bfd *abfd) 406 { 407 const char *filename; 408 409 if (full_pathname) 410 return file; 411 412 filename = lbasename (file); 413 414 if (ar_truncate 415 && abfd != NULL 416 && strlen (filename) > abfd->xvec->ar_max_namelen) 417 { 418 char *s; 419 420 /* Space leak. */ 421 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1); 422 memcpy (s, filename, abfd->xvec->ar_max_namelen); 423 s[abfd->xvec->ar_max_namelen] = '\0'; 424 filename = s; 425 } 426 427 return filename; 428 } 429 430 /* Remove any output file. This is only called via xatexit. */ 431 432 static char *output_filename = NULL; 433 static FILE *output_file = NULL; 434 435 static void 436 remove_output (void) 437 { 438 if (output_filename != NULL) 439 { 440 if (output_file != NULL) 441 fclose (output_file); 442 unlink_if_ordinary (output_filename); 443 free (output_filename); 444 output_filename = NULL; 445 } 446 } 447 448 static char ** 449 decode_options (int argc, char **argv) 450 { 451 int c; 452 453 /* Convert old-style ar call by exploding option element and rearranging 454 options accordingly. */ 455 456 restart: 457 if (argc > 1 && argv[1][0] != '-') 458 { 459 int new_argc; /* argc value for rearranged arguments */ 460 char **new_argv; /* argv value for rearranged arguments */ 461 char *const *in; /* cursor into original argv */ 462 char **out; /* cursor into rearranged argv */ 463 const char *letter; /* cursor into old option letters */ 464 char buffer[3]; /* constructed option buffer */ 465 466 /* Initialize a constructed option. */ 467 468 buffer[0] = '-'; 469 buffer[2] = '\0'; 470 471 /* Allocate a new argument array, and copy program name in it. */ 472 473 new_argc = argc - 1 + strlen (argv[1]); 474 new_argv = xmalloc ((new_argc + 1) * sizeof (*argv)); 475 in = argv; 476 out = new_argv; 477 *out++ = *in++; 478 479 /* Copy each old letter option as a separate option. */ 480 481 for (letter = *in++; *letter; letter++) 482 { 483 buffer[1] = *letter; 484 *out++ = xstrdup (buffer); 485 } 486 487 /* Copy all remaining options. */ 488 489 while (in < argv + argc) 490 *out++ = *in++; 491 *out = NULL; 492 493 /* Replace the old option list by the new one. */ 494 495 argc = new_argc; 496 argv = new_argv; 497 } 498 499 while ((c = getopt_long (argc, argv, "hdmpqrtxl:coOVsSuvabiMNfPTDU", 500 long_options, NULL)) != EOF) 501 { 502 switch (c) 503 { 504 case 'd': 505 case 'm': 506 case 'p': 507 case 'q': 508 case 'r': 509 case 't': 510 case 'x': 511 if (operation != none) 512 fatal (_("two different operation options specified")); 513 break; 514 } 515 516 switch (c) 517 { 518 case 'h': 519 show_help = 1; 520 break; 521 case 'd': 522 operation = del; 523 operation_alters_arch = true; 524 break; 525 case 'm': 526 operation = move; 527 operation_alters_arch = true; 528 break; 529 case 'p': 530 operation = print_files; 531 break; 532 case 'q': 533 operation = quick_append; 534 operation_alters_arch = true; 535 break; 536 case 'r': 537 operation = replace; 538 operation_alters_arch = true; 539 break; 540 case 't': 541 operation = print_table; 542 break; 543 case 'x': 544 operation = extract; 545 break; 546 case 'l': 547 if (libdeps != NULL) 548 fatal (_("libdeps specified more than once")); 549 libdeps = optarg; 550 break; 551 case 'c': 552 silent_create = 1; 553 break; 554 case 'o': 555 preserve_dates = 1; 556 break; 557 case 'O': 558 display_offsets = 1; 559 break; 560 case 'V': 561 show_version = true; 562 break; 563 case 's': 564 write_armap = 1; 565 break; 566 case 'S': 567 write_armap = -1; 568 break; 569 case 'u': 570 newer_only = 1; 571 break; 572 case 'v': 573 verbose = 1; 574 break; 575 case 'a': 576 postype = pos_after; 577 break; 578 case 'b': 579 postype = pos_before; 580 break; 581 case 'i': 582 postype = pos_before; 583 break; 584 case 'M': 585 mri_mode = 1; 586 break; 587 case 'N': 588 counted_name_mode = true; 589 break; 590 case 'f': 591 ar_truncate = true; 592 break; 593 case 'P': 594 full_pathname = true; 595 break; 596 case 'T': 597 make_thin_archive = true; 598 break; 599 case 'D': 600 deterministic = true; 601 break; 602 case 'U': 603 deterministic = false; 604 break; 605 case OPTION_PLUGIN: 606 #if BFD_SUPPORTS_PLUGINS 607 bfd_plugin_set_plugin (optarg); 608 #else 609 fprintf (stderr, _("sorry - this program has been built without plugin support\n")); 610 xexit (1); 611 #endif 612 break; 613 case OPTION_TARGET: 614 target = optarg; 615 break; 616 case OPTION_OUTPUT: 617 output_dir = optarg; 618 break; 619 case 0: /* A long option that just sets a flag. */ 620 break; 621 default: 622 usage (0); 623 } 624 } 625 626 /* PR 13256: Allow for the possibility that the first command line option 627 started with a dash (eg --plugin) but then the following option(s) are 628 old style, non-dash-prefixed versions. */ 629 if (operation == none && write_armap != 1 && !mri_mode 630 && optind > 0 && optind < argc) 631 { 632 argv += (optind - 1); 633 argc -= (optind - 1); 634 optind = 0; 635 goto restart; 636 } 637 638 return &argv[optind]; 639 } 640 641 /* If neither -D nor -U was specified explicitly, 642 then use the configured default. */ 643 static void 644 default_deterministic (void) 645 { 646 if (deterministic < 0) 647 deterministic = DEFAULT_AR_DETERMINISTIC; 648 } 649 650 static void 651 ranlib_main (int argc, char **argv) 652 { 653 int arg_index, status = 0; 654 bool touch = false; 655 int c; 656 657 while ((c = getopt_long (argc, argv, "DhHUvVt", long_options, NULL)) != EOF) 658 { 659 switch (c) 660 { 661 case 'D': 662 deterministic = true; 663 break; 664 case 'U': 665 deterministic = false; 666 break; 667 case 'h': 668 case 'H': 669 show_help = 1; 670 break; 671 case 't': 672 touch = true; 673 break; 674 case 'v': 675 case 'V': 676 show_version = 1; 677 break; 678 679 /* PR binutils/13493: Support plugins. */ 680 case OPTION_PLUGIN: 681 #if BFD_SUPPORTS_PLUGINS 682 bfd_plugin_set_plugin (optarg); 683 #else 684 fprintf (stderr, _("sorry - this program has been built without plugin support\n")); 685 xexit (1); 686 #endif 687 break; 688 } 689 } 690 691 if (argc < 2) 692 ranlib_usage (0); 693 694 if (show_help) 695 ranlib_usage (1); 696 697 if (show_version) 698 print_version ("ranlib"); 699 700 default_deterministic (); 701 702 arg_index = optind; 703 704 while (arg_index < argc) 705 { 706 if (! touch) 707 status |= ranlib_only (argv[arg_index]); 708 else 709 status |= ranlib_touch (argv[arg_index]); 710 ++arg_index; 711 } 712 713 xexit (status); 714 } 715 716 int main (int, char **); 717 718 int 719 main (int argc, char **argv) 720 { 721 int arg_index; 722 char **files; 723 int file_count; 724 char *inarch_filename; 725 int i; 726 727 #ifdef HAVE_LC_MESSAGES 728 setlocale (LC_MESSAGES, ""); 729 #endif 730 setlocale (LC_CTYPE, ""); 731 bindtextdomain (PACKAGE, LOCALEDIR); 732 textdomain (PACKAGE); 733 734 program_name = argv[0]; 735 xmalloc_set_program_name (program_name); 736 bfd_set_error_program_name (program_name); 737 #if BFD_SUPPORTS_PLUGINS 738 bfd_plugin_set_program_name (program_name); 739 #endif 740 741 expandargv (&argc, &argv); 742 743 #ifndef is_ranlib 744 if (is_ranlib < 0) 745 { 746 size_t l = strlen (program_name); 747 748 #ifdef HAVE_DOS_BASED_FILE_SYSTEM 749 /* Drop the .exe suffix, if any. */ 750 if (l > 4 && FILENAME_CMP (program_name + l - 4, ".exe") == 0) 751 { 752 l -= 4; 753 program_name[l] = '\0'; 754 } 755 #endif 756 is_ranlib = (l >= 6 && 757 FILENAME_CMP (program_name + l - 6, "ranlib") == 0); 758 } 759 #endif 760 761 if (bfd_init () != BFD_INIT_MAGIC) 762 fatal (_("fatal error: libbfd ABI mismatch")); 763 set_default_bfd_target (); 764 765 xatexit (remove_output); 766 767 for (i = 1; i < argc; i++) 768 if (! ar_emul_parse_arg (argv[i])) 769 break; 770 argv += (i - 1); 771 argc -= (i - 1); 772 773 if (is_ranlib) 774 ranlib_main (argc, argv); 775 776 if (argc < 2) 777 usage (0); 778 779 argv = decode_options (argc, argv); 780 781 if (show_help) 782 usage (1); 783 784 if (show_version) 785 print_version ("ar"); 786 787 arg_index = 0; 788 789 if (mri_mode) 790 { 791 default_deterministic (); 792 mri_emul (); 793 } 794 else 795 { 796 bfd *arch; 797 798 /* Fail if no files are specified on the command line. 799 (But not for MRI mode which allows for reading arguments 800 and filenames from stdin). */ 801 if (argv[arg_index] == NULL) 802 usage (0); 803 804 /* We don't use do_quick_append any more. Too many systems 805 expect ar to always rebuild the symbol table even when q is 806 used. */ 807 808 /* We can't write an armap when using ar q, so just do ar r 809 instead. */ 810 if (operation == quick_append && write_armap) 811 operation = replace; 812 813 if ((operation == none || operation == print_table) 814 && write_armap == 1) 815 xexit (ranlib_only (argv[arg_index])); 816 817 if (operation == none) 818 fatal (_("no operation specified")); 819 820 if (newer_only && operation != replace) 821 fatal (_("`u' is only meaningful with the `r' option.")); 822 823 if (newer_only && deterministic > 0) 824 non_fatal (_("`u' is not meaningful with the `D' option - replacement will always happen.")); 825 826 if (newer_only && deterministic < 0 && DEFAULT_AR_DETERMINISTIC) 827 non_fatal (_("\ 828 `u' modifier ignored since `D' is the default (see `U')")); 829 830 default_deterministic (); 831 832 if (postype != pos_default) 833 { 834 posname = argv[arg_index++]; 835 if (posname == NULL) 836 fatal (_("missing position arg.")); 837 } 838 839 if (counted_name_mode) 840 { 841 if (operation != extract && operation != del) 842 fatal (_("`N' is only meaningful with the `x' and `d' options.")); 843 if (argv[arg_index] == NULL) 844 fatal (_("`N' missing value.")); 845 counted_name_counter = atoi (argv[arg_index++]); 846 if (counted_name_counter <= 0) 847 fatal (_("Value for `N' must be positive.")); 848 } 849 850 inarch_filename = argv[arg_index++]; 851 if (inarch_filename == NULL) 852 usage (0); 853 854 for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++) 855 continue; 856 857 files = (file_count > 0) ? argv + arg_index : NULL; 858 859 arch = open_inarch (inarch_filename, 860 files == NULL ? (char *) NULL : files[0]); 861 862 if (operation == extract && bfd_is_thin_archive (arch)) 863 fatal (_("`x' cannot be used on thin archives.")); 864 865 if (libdeps != NULL) 866 { 867 char **new_files; 868 bfd_size_type reclen = strlen (libdeps) + 1; 869 870 /* Create a bfd to contain the dependencies. 871 It inherits its type from arch, but we must set the type to 872 "binary" otherwise bfd_write() will fail. After writing, we 873 must set the type back to default otherwise adding it to the 874 archive will fail. */ 875 libdeps_bfd = bfd_create (LIBDEPS, arch); 876 if (libdeps_bfd == NULL) 877 fatal (_("Cannot create libdeps record.")); 878 879 if (bfd_find_target ("binary", libdeps_bfd) == NULL) 880 fatal (_("Cannot set libdeps record type to binary.")); 881 882 if (! bfd_set_format (libdeps_bfd, bfd_object)) 883 fatal (_("Cannot set libdeps object format.")); 884 885 if (! bfd_make_writable (libdeps_bfd)) 886 fatal (_("Cannot make libdeps object writable.")); 887 888 if (bfd_write (libdeps, reclen, libdeps_bfd) != reclen) 889 fatal (_("Cannot write libdeps record.")); 890 891 if (! bfd_make_readable (libdeps_bfd)) 892 fatal (_("Cannot make libdeps object readable.")); 893 894 if (bfd_find_target (plugin_target, libdeps_bfd) == NULL) 895 fatal (_("Cannot reset libdeps record type.")); 896 897 /* Insert our libdeps record in 2nd slot of the list of files 898 being operated on. We shouldn't use 1st slot, but we want 899 to avoid having to search all the way to the end of an 900 archive with a large number of members at link time. */ 901 new_files = xmalloc ((file_count + 2) * sizeof (*new_files)); 902 if (file_count) 903 { 904 new_files[0] = files[0]; 905 memcpy (new_files + 1, files, file_count * sizeof (*files)); 906 } 907 new_files[file_count != 0] = LIBDEPS; 908 file_count++; 909 new_files[file_count] = NULL; 910 files = new_files; 911 } 912 913 switch (operation) 914 { 915 case print_table: 916 map_over_members (arch, print_descr, files, file_count); 917 break; 918 919 case print_files: 920 map_over_members (arch, print_contents, files, file_count); 921 break; 922 923 case extract: 924 map_over_members (arch, extract_file, files, file_count); 925 break; 926 927 case del: 928 if (files != NULL) 929 delete_members (arch, files); 930 else 931 { 932 free (output_filename); 933 output_filename = NULL; 934 } 935 break; 936 937 case move: 938 /* PR 12558: Creating and moving at the same time does 939 not make sense. Just create the archive instead. */ 940 if (! silent_create) 941 { 942 if (files != NULL) 943 move_members (arch, files); 944 else 945 { 946 free (output_filename); 947 output_filename = NULL; 948 } 949 break; 950 } 951 /* Fall through. */ 952 953 case replace: 954 case quick_append: 955 if (files != NULL || write_armap > 0) 956 replace_members (arch, files, operation == quick_append); 957 else 958 { 959 free (output_filename); 960 output_filename = NULL; 961 } 962 break; 963 964 /* Shouldn't happen! */ 965 default: 966 /* xgettext:c-format */ 967 fatal (_("internal error -- this option not implemented")); 968 } 969 } 970 971 xexit (0); 972 return 0; 973 } 974 975 bfd * 976 open_inarch (const char *archive_filename, const char *file) 977 { 978 bfd **last_one; 979 bfd *next_one; 980 struct stat sbuf; 981 bfd *arch; 982 char **matching; 983 984 bfd_set_error (bfd_error_no_error); 985 986 if (target == NULL) 987 target = plugin_target; 988 989 if (stat (archive_filename, &sbuf) != 0) 990 { 991 #if !defined(__GO32__) || defined(__DJGPP__) 992 993 /* FIXME: I don't understand why this fragment was ifndef'ed 994 away for __GO32__; perhaps it was in the days of DJGPP v1.x. 995 stat() works just fine in v2.x, so I think this should be 996 removed. For now, I enable it for DJGPP v2. -- EZ. */ 997 998 /* KLUDGE ALERT! Temporary fix until I figger why 999 stat() is wrong ... think it's buried in GO32's IDT - Jax */ 1000 if (errno != ENOENT) 1001 bfd_fatal (archive_filename); 1002 #endif 1003 1004 if (!operation_alters_arch) 1005 { 1006 fprintf (stderr, "%s: ", program_name); 1007 perror (archive_filename); 1008 maybequit (); 1009 return NULL; 1010 } 1011 1012 /* If the target isn't set, try to figure out the target to use 1013 for the archive from the first object on the list. */ 1014 if (target == NULL && file != NULL) 1015 { 1016 bfd *obj; 1017 1018 obj = bfd_openr (file, target); 1019 if (obj != NULL) 1020 { 1021 if (bfd_check_format (obj, bfd_object) 1022 && bfd_target_supports_archives (obj)) 1023 target = bfd_get_target (obj); 1024 (void) bfd_close (obj); 1025 } 1026 } 1027 1028 /* If we die creating a new archive, don't leave it around. */ 1029 output_filename = xstrdup (archive_filename); 1030 1031 /* Create an empty archive. */ 1032 arch = bfd_openw (archive_filename, target); 1033 if (arch == NULL 1034 || ! bfd_set_format (arch, bfd_archive) 1035 || ! bfd_close (arch)) 1036 bfd_fatal (archive_filename); 1037 else if (!silent_create) 1038 non_fatal (_("creating %s"), archive_filename); 1039 } 1040 1041 arch = bfd_openr (archive_filename, target); 1042 if (arch == NULL) 1043 { 1044 bloser: 1045 bfd_fatal (archive_filename); 1046 } 1047 1048 if (! bfd_check_format_matches (arch, bfd_archive, &matching)) 1049 { 1050 bfd_nonfatal (archive_filename); 1051 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1052 list_matching_formats (matching); 1053 xexit (1); 1054 } 1055 1056 if ((operation == replace || operation == quick_append) 1057 && bfd_openr_next_archived_file (arch, NULL) != NULL) 1058 { 1059 /* PR 15140: Catch attempts to convert a normal 1060 archive into a thin archive or vice versa. */ 1061 if (make_thin_archive && ! bfd_is_thin_archive (arch)) 1062 { 1063 fatal (_("Cannot convert existing library %s to thin format"), 1064 bfd_get_filename (arch)); 1065 goto bloser; 1066 } 1067 else if (! make_thin_archive && bfd_is_thin_archive (arch)) 1068 { 1069 fatal (_("Cannot convert existing thin library %s to normal format"), 1070 bfd_get_filename (arch)); 1071 goto bloser; 1072 } 1073 } 1074 1075 last_one = &(arch->archive_next); 1076 /* Read all the contents right away, regardless. */ 1077 for (next_one = bfd_openr_next_archived_file (arch, NULL); 1078 next_one; 1079 next_one = bfd_openr_next_archived_file (arch, next_one)) 1080 { 1081 *last_one = next_one; 1082 last_one = &next_one->archive_next; 1083 } 1084 *last_one = (bfd *) NULL; 1085 if (bfd_get_error () != bfd_error_no_more_archived_files) 1086 goto bloser; 1087 return arch; 1088 } 1089 1090 static void 1091 print_contents (bfd *abfd) 1092 { 1093 bfd_size_type ncopied = 0; 1094 bfd_size_type size; 1095 char *cbuf = (char *) xmalloc (BUFSIZE); 1096 struct stat buf; 1097 1098 if (bfd_stat_arch_elt (abfd, &buf) != 0) 1099 /* xgettext:c-format */ 1100 fatal (_("internal stat error on %s"), bfd_get_filename (abfd)); 1101 1102 if (verbose) 1103 printf ("\n<%s>\n\n", bfd_get_filename (abfd)); 1104 1105 if (bfd_seek (abfd, 0, SEEK_SET) != 0) 1106 bfd_fatal (bfd_get_filename (abfd)); 1107 1108 size = buf.st_size; 1109 while (ncopied < size) 1110 { 1111 bfd_size_type nread; 1112 bfd_size_type tocopy = size - ncopied; 1113 1114 if (tocopy > BUFSIZE) 1115 tocopy = BUFSIZE; 1116 1117 nread = bfd_read (cbuf, tocopy, abfd); 1118 if (nread != tocopy) 1119 /* xgettext:c-format */ 1120 fatal (_("%s is not a valid archive"), 1121 bfd_get_filename (abfd->my_archive)); 1122 1123 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the 1124 return value to bfd_size_type to avoid comparison between signed and 1125 unsigned values. */ 1126 if ((bfd_size_type) fwrite (cbuf, 1, nread, stdout) != nread) 1127 fatal ("stdout: %s", strerror (errno)); 1128 ncopied += tocopy; 1129 } 1130 free (cbuf); 1131 } 1132 1133 1134 static FILE * open_output_file (bfd *) ATTRIBUTE_RETURNS_NONNULL; 1135 1136 static FILE * 1137 open_output_file (bfd * abfd) 1138 { 1139 char *alloc = xstrdup (bfd_get_filename (abfd)); 1140 1141 output_filename = alloc; 1142 1143 /* PR binutils/17533: Do not allow directory traversal 1144 outside of the current directory tree - unless the 1145 user has explicitly specified an output directory. */ 1146 if (! is_valid_archive_path (output_filename)) 1147 { 1148 char * base = (char *) lbasename (output_filename); 1149 1150 non_fatal (_("illegal output pathname for archive member: %s, using '%s' instead"), 1151 output_filename, base); 1152 output_filename = xstrdup (base); 1153 free (alloc); 1154 alloc = output_filename; 1155 } 1156 1157 if (output_dir) 1158 { 1159 size_t len = strlen (output_dir); 1160 1161 if (len > 0) 1162 { 1163 if (IS_DIR_SEPARATOR (output_dir [len - 1])) 1164 output_filename = concat (output_dir, output_filename, NULL); 1165 else 1166 output_filename = concat (output_dir, "/", output_filename, NULL); 1167 } 1168 free (alloc); 1169 } 1170 1171 if (verbose) 1172 printf ("x - %s\n", output_filename); 1173 1174 FILE * ostream = fopen (output_filename, FOPEN_WB); 1175 if (ostream == NULL) 1176 { 1177 perror (output_filename); 1178 xexit (1); 1179 } 1180 1181 return ostream; 1182 } 1183 1184 /* Extract a member of the archive into its own file. 1185 1186 We defer opening the new file until after we have read a BUFSIZ chunk of the 1187 old one, since we know we have just read the archive header for the old 1188 one. Since most members are shorter than BUFSIZ, this means we will read 1189 the old header, read the old data, write a new inode for the new file, and 1190 write the new data, and be done. This 'optimization' is what comes from 1191 sitting next to a bare disk and hearing it every time it seeks. -- Gnu 1192 Gilmore */ 1193 1194 void 1195 extract_file (bfd *abfd) 1196 { 1197 bfd_size_type size; 1198 struct stat buf; 1199 1200 if (preserve_dates) 1201 memset (&buf, 0, sizeof (buf)); 1202 1203 if (bfd_stat_arch_elt (abfd, &buf) != 0) 1204 /* xgettext:c-format */ 1205 fatal (_("internal stat error on %s"), bfd_get_filename (abfd)); 1206 size = buf.st_size; 1207 1208 if (bfd_seek (abfd, 0, SEEK_SET) != 0) 1209 bfd_fatal (bfd_get_filename (abfd)); 1210 1211 output_file = NULL; 1212 if (size == 0) 1213 { 1214 output_file = open_output_file (abfd); 1215 } 1216 else 1217 { 1218 bfd_size_type ncopied = 0; 1219 char *cbuf = (char *) xmalloc (BUFSIZE); 1220 1221 while (ncopied < size) 1222 { 1223 bfd_size_type nread, tocopy; 1224 1225 tocopy = size - ncopied; 1226 if (tocopy > BUFSIZE) 1227 tocopy = BUFSIZE; 1228 1229 nread = bfd_read (cbuf, tocopy, abfd); 1230 if (nread != tocopy) 1231 /* xgettext:c-format */ 1232 fatal (_("%s is not a valid archive"), 1233 bfd_get_filename (abfd->my_archive)); 1234 1235 /* See comment above; this saves disk arm motion. */ 1236 if (output_file == NULL) 1237 output_file = open_output_file (abfd); 1238 1239 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast 1240 the return value to bfd_size_type to avoid comparison between 1241 signed and unsigned values. */ 1242 if ((bfd_size_type) fwrite (cbuf, 1, nread, output_file) != nread) 1243 fatal ("%s: %s", output_filename, strerror (errno)); 1244 1245 ncopied += tocopy; 1246 } 1247 1248 free (cbuf); 1249 } 1250 1251 fclose (output_file); 1252 1253 output_file = NULL; 1254 1255 chmod (output_filename, buf.st_mode); 1256 1257 if (preserve_dates) 1258 { 1259 /* Set access time to modification time. Only st_mtime is 1260 initialized by bfd_stat_arch_elt. */ 1261 buf.st_atime = buf.st_mtime; 1262 set_times (output_filename, &buf); 1263 } 1264 1265 free (output_filename); 1266 output_filename = NULL; 1267 } 1268 1269 static void 1270 write_archive (bfd *iarch) 1271 { 1272 bfd *obfd; 1273 const char *old_name; 1274 char *new_name; 1275 bfd *contents_head = iarch->archive_next; 1276 int tmpfd = -1; 1277 1278 old_name = bfd_get_filename (iarch); 1279 new_name = make_tempname (old_name, &tmpfd); 1280 1281 if (new_name == NULL) 1282 bfd_fatal (_("could not create temporary file whilst writing archive")); 1283 1284 free (output_filename); 1285 output_filename = new_name; 1286 1287 obfd = bfd_fdopenw (new_name, bfd_get_target (iarch), tmpfd); 1288 1289 if (obfd == NULL) 1290 { 1291 close (tmpfd); 1292 bfd_fatal (old_name); 1293 } 1294 1295 bfd_set_format (obfd, bfd_archive); 1296 1297 /* Request writing the archive symbol table unless we've 1298 been explicitly requested not to. */ 1299 obfd->has_armap = write_armap >= 0; 1300 1301 if (ar_truncate) 1302 { 1303 /* This should really use bfd_set_file_flags, but that rejects 1304 archives. */ 1305 obfd->flags |= BFD_TRADITIONAL_FORMAT; 1306 } 1307 1308 if (deterministic) 1309 obfd->flags |= BFD_DETERMINISTIC_OUTPUT; 1310 1311 if (full_pathname) 1312 obfd->flags |= BFD_ARCHIVE_FULL_PATH; 1313 1314 if (make_thin_archive || bfd_is_thin_archive (iarch)) 1315 bfd_set_thin_archive (obfd, true); 1316 1317 if (!bfd_set_archive_head (obfd, contents_head)) 1318 bfd_fatal (old_name); 1319 1320 tmpfd = dup (tmpfd); 1321 if (!bfd_close (obfd)) 1322 bfd_fatal (old_name); 1323 1324 output_filename = NULL; 1325 old_name = xstrdup (old_name); 1326 /* We don't care if this fails; we might be creating the archive. */ 1327 bfd_close (iarch); 1328 1329 int ret = smart_rename (new_name, old_name, tmpfd, NULL, false); 1330 free ((char *) old_name); 1331 free (new_name); 1332 if (ret != 0) 1333 xexit (1); 1334 } 1335 1336 /* Return a pointer to the pointer to the entry which should be rplacd'd 1337 into when altering. DEFAULT_POS should be how to interpret pos_default, 1338 and should be a pos value. */ 1339 1340 static bfd ** 1341 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname) 1342 { 1343 bfd **after_bfd = contents; 1344 enum pos realpos; 1345 const char *realposname; 1346 1347 if (postype == pos_default) 1348 { 1349 realpos = default_pos; 1350 realposname = default_posname; 1351 } 1352 else 1353 { 1354 realpos = postype; 1355 realposname = posname; 1356 } 1357 1358 if (realpos == pos_end) 1359 { 1360 while (*after_bfd) 1361 after_bfd = &((*after_bfd)->archive_next); 1362 } 1363 else 1364 { 1365 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next) 1366 if (FILENAME_CMP (bfd_get_filename (*after_bfd), realposname) == 0) 1367 { 1368 if (realpos == pos_after) 1369 after_bfd = &(*after_bfd)->archive_next; 1370 break; 1371 } 1372 } 1373 return after_bfd; 1374 } 1375 1376 static void 1377 delete_members (bfd *arch, char **files_to_delete) 1378 { 1379 bfd **current_ptr_ptr; 1380 bool found; 1381 bool something_changed = false; 1382 int match_count; 1383 1384 for (; *files_to_delete != NULL; ++files_to_delete) 1385 { 1386 /* In a.out systems, the armap is optional. It's also called 1387 __.SYMDEF. So if the user asked to delete it, we should remember 1388 that fact. This isn't quite right for COFF systems (where 1389 __.SYMDEF might be regular member), but it's very unlikely 1390 to be a problem. FIXME */ 1391 1392 if (!strcmp (*files_to_delete, "__.SYMDEF")) 1393 { 1394 arch->has_armap = false; 1395 write_armap = -1; 1396 continue; 1397 } 1398 1399 found = false; 1400 match_count = 0; 1401 current_ptr_ptr = &(arch->archive_next); 1402 while (*current_ptr_ptr) 1403 { 1404 if (FILENAME_CMP (normalize (*files_to_delete, arch), 1405 bfd_get_filename (*current_ptr_ptr)) == 0) 1406 { 1407 ++match_count; 1408 if (counted_name_mode 1409 && match_count != counted_name_counter) 1410 { 1411 /* Counting, and didn't match on count; go on to the 1412 next one. */ 1413 } 1414 else 1415 { 1416 found = true; 1417 something_changed = true; 1418 if (verbose) 1419 printf ("d - %s\n", 1420 *files_to_delete); 1421 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next); 1422 goto next_file; 1423 } 1424 } 1425 1426 current_ptr_ptr = &((*current_ptr_ptr)->archive_next); 1427 } 1428 1429 if (verbose && !found) 1430 { 1431 /* xgettext:c-format */ 1432 printf (_("No member named `%s'\n"), *files_to_delete); 1433 } 1434 next_file: 1435 ; 1436 } 1437 1438 if (something_changed) 1439 write_archive (arch); 1440 else 1441 { 1442 free (output_filename); 1443 output_filename = NULL; 1444 } 1445 } 1446 1447 1448 /* Reposition existing members within an archive */ 1449 1450 static void 1451 move_members (bfd *arch, char **files_to_move) 1452 { 1453 bfd **after_bfd; /* New entries go after this one */ 1454 bfd **current_ptr_ptr; /* cdr pointer into contents */ 1455 1456 for (; *files_to_move; ++files_to_move) 1457 { 1458 current_ptr_ptr = &(arch->archive_next); 1459 while (*current_ptr_ptr) 1460 { 1461 bfd *current_ptr = *current_ptr_ptr; 1462 if (FILENAME_CMP (normalize (*files_to_move, arch), 1463 bfd_get_filename (current_ptr)) == 0) 1464 { 1465 /* Move this file to the end of the list - first cut from 1466 where it is. */ 1467 bfd *link_bfd; 1468 *current_ptr_ptr = current_ptr->archive_next; 1469 1470 /* Now glue to end */ 1471 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL); 1472 link_bfd = *after_bfd; 1473 *after_bfd = current_ptr; 1474 current_ptr->archive_next = link_bfd; 1475 1476 if (verbose) 1477 printf ("m - %s\n", *files_to_move); 1478 1479 goto next_file; 1480 } 1481 1482 current_ptr_ptr = &((*current_ptr_ptr)->archive_next); 1483 } 1484 /* xgettext:c-format */ 1485 fatal (_("no entry %s in archive %s!"), *files_to_move, 1486 bfd_get_filename (arch)); 1487 1488 next_file:; 1489 } 1490 1491 write_archive (arch); 1492 } 1493 1494 /* Ought to default to replacing in place, but this is existing practice! */ 1495 1496 static void 1497 replace_members (bfd *arch, char **files_to_move, bool quick) 1498 { 1499 bool changed = false; 1500 bfd **after_bfd; /* New entries go after this one. */ 1501 bfd *current; 1502 bfd **current_ptr; 1503 1504 while (files_to_move && *files_to_move) 1505 { 1506 if (! quick) 1507 { 1508 current_ptr = &arch->archive_next; 1509 while (*current_ptr) 1510 { 1511 current = *current_ptr; 1512 1513 /* For compatibility with existing ar programs, we 1514 permit the same file to be added multiple times. */ 1515 if (FILENAME_CMP (normalize (*files_to_move, arch), 1516 normalize (bfd_get_filename (current), arch)) == 0 1517 && current->arelt_data != NULL) 1518 { 1519 bool replaced; 1520 1521 if (newer_only) 1522 { 1523 struct stat fsbuf, asbuf; 1524 1525 if (stat (*files_to_move, &fsbuf) != 0) 1526 { 1527 if (errno != ENOENT) 1528 bfd_fatal (*files_to_move); 1529 goto next_file; 1530 } 1531 1532 if (bfd_stat_arch_elt (current, &asbuf) != 0) 1533 /* xgettext:c-format */ 1534 fatal (_("internal stat error on %s"), 1535 bfd_get_filename (current)); 1536 1537 if (fsbuf.st_mtime <= asbuf.st_mtime) 1538 /* A note about deterministic timestamps: In an 1539 archive created in a determistic manner the 1540 individual elements will either have a timestamp 1541 of 0 or SOURCE_DATE_EPOCH, depending upon the 1542 method used. This will be the value retrieved 1543 by bfd_stat_arch_elt(). 1544 1545 The timestamp in fsbuf.st_mtime however will 1546 definitely be greater than 0, and it is unlikely 1547 to be less than SOURCE_DATE_EPOCH. (FIXME: 1548 should we test for this case case and issue an 1549 error message ?) 1550 1551 So in either case fsbuf.st_mtime > asbuf.st_time 1552 and hence the incoming file will replace the 1553 current file. Which is what should be expected to 1554 happen. Deterministic archives have no real sense 1555 of the time/date when their elements were created, 1556 and so any updates to the archive should always 1557 result in replaced files. */ 1558 goto next_file; 1559 } 1560 1561 after_bfd = get_pos_bfd (&arch->archive_next, pos_after, 1562 bfd_get_filename (current)); 1563 if (libdeps_bfd != NULL 1564 && FILENAME_CMP (normalize (*files_to_move, arch), 1565 LIBDEPS) == 0) 1566 { 1567 replaced = ar_emul_replace_bfd (after_bfd, libdeps_bfd, 1568 verbose); 1569 } 1570 else 1571 { 1572 replaced = ar_emul_replace (after_bfd, *files_to_move, 1573 target, verbose); 1574 } 1575 if (replaced) 1576 { 1577 /* Snip out this entry from the chain. */ 1578 *current_ptr = (*current_ptr)->archive_next; 1579 changed = true; 1580 } 1581 1582 goto next_file; 1583 } 1584 current_ptr = &(current->archive_next); 1585 } 1586 } 1587 1588 /* Add to the end of the archive. */ 1589 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL); 1590 1591 if (libdeps_bfd != NULL 1592 && FILENAME_CMP (normalize (*files_to_move, arch), LIBDEPS) == 0) 1593 { 1594 changed |= ar_emul_append_bfd (after_bfd, libdeps_bfd, 1595 verbose, make_thin_archive); 1596 } 1597 else 1598 { 1599 changed |= ar_emul_append (after_bfd, *files_to_move, target, 1600 verbose, make_thin_archive); 1601 } 1602 1603 next_file:; 1604 1605 files_to_move++; 1606 } 1607 1608 if (changed) 1609 write_archive (arch); 1610 else 1611 { 1612 free (output_filename); 1613 output_filename = NULL; 1614 } 1615 } 1616 1617 static int 1618 ranlib_only (const char *archname) 1619 { 1620 bfd *arch; 1621 1622 if (get_file_size (archname) < 1) 1623 return 1; 1624 write_armap = 1; 1625 arch = open_inarch (archname, (char *) NULL); 1626 if (arch == NULL) 1627 xexit (1); 1628 write_archive (arch); 1629 return 0; 1630 } 1631 1632 /* Update the timestamp of the symbol map of an archive. */ 1633 1634 static int 1635 ranlib_touch (const char *archname) 1636 { 1637 #ifdef __GO32__ 1638 /* I don't think updating works on go32. */ 1639 ranlib_only (archname); 1640 #else 1641 int f; 1642 bfd *arch; 1643 char **matching; 1644 1645 if (get_file_size (archname) < 1) 1646 return 1; 1647 f = open (archname, O_RDWR | O_BINARY, 0); 1648 if (f < 0) 1649 { 1650 bfd_set_error (bfd_error_system_call); 1651 bfd_fatal (archname); 1652 } 1653 1654 arch = bfd_fdopenr (archname, (const char *) NULL, f); 1655 if (arch == NULL) 1656 bfd_fatal (archname); 1657 if (! bfd_check_format_matches (arch, bfd_archive, &matching)) 1658 { 1659 bfd_nonfatal (archname); 1660 if (bfd_get_error () == bfd_error_file_ambiguously_recognized) 1661 list_matching_formats (matching); 1662 xexit (1); 1663 } 1664 1665 if (! bfd_has_map (arch)) 1666 /* xgettext:c-format */ 1667 fatal (_("%s: no archive map to update"), archname); 1668 1669 if (deterministic) 1670 arch->flags |= BFD_DETERMINISTIC_OUTPUT; 1671 1672 bfd_update_armap_timestamp (arch); 1673 1674 if (! bfd_close (arch)) 1675 bfd_fatal (archname); 1676 #endif 1677 return 0; 1678 } 1679 1680 /* Things which are interesting to map over all or some of the files: */ 1681 1682 static void 1683 print_descr (bfd *abfd) 1684 { 1685 print_arelt_descr (stdout, abfd, verbose, display_offsets); 1686 } 1687