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