1 /* ar.c - Archive modify and extract.
2 Copyright (C) 1991-2021 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
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. */
29 #include "libiberty.h"
35 #include "filenames.h"
37 #include "plugin-api.h"
42 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
44 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
47 /* Static declarations. */
49 static void mri_emul (void);
50 static const char *normalize (const char *, bfd
*);
51 static void remove_output (void);
52 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
53 static void print_contents (bfd
* member
);
54 static void delete_members (bfd
*, char **files_to_delete
);
56 static void move_members (bfd
*, char **files_to_move
);
57 static void replace_members
58 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
59 static void print_descr (bfd
* abfd
);
60 static void write_archive (bfd
*);
61 static int ranlib_only (const char *archname
);
62 static int ranlib_touch (const char *archname
);
63 static void usage (int);
65 /** Globals and flags. */
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. */
74 /* Nonzero means don't warn about creating the archive file if necessary. */
75 int silent_create
= 0;
77 /* Nonzero means describe each action performed. */
80 /* Nonzero means display offsets of files in the archive. */
81 int display_offsets
= 0;
83 /* Nonzero means preserve dates of members when extracting them. */
84 int preserve_dates
= 0;
86 /* Nonzero means don't replace existing members whose dates are more recent
87 than the corresponding files. */
90 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
91 member). -1 means we've been explicitly asked to not write a symbol table;
92 +1 means we've been explicitly asked to write it;
94 Traditionally, the default in BSD has been to not write the table.
95 However, for POSIX.2 compliance the default is now to write a symbol table
96 if any of the members are object files. */
99 /* Operate in deterministic mode: write zero for timestamps, uids,
100 and gids for archive members and the archive symbol table, and write
101 consistent file modes. */
102 int deterministic
= -1; /* Determinism indeterminate. */
104 /* Nonzero means it's the name of an existing member; position new or moved
105 files with respect to this one. */
106 char *posname
= NULL
;
108 /* Sez how to use `posname': pos_before means position before that member.
109 pos_after means position after that member. pos_end means always at end.
110 pos_default means default appropriately. For the latter two, `posname'
111 should also be zero. */
114 pos_default
, pos_before
, pos_after
, pos_end
115 } postype
= pos_default
;
119 none
= 0, del
, replace
, print_table
,
120 print_files
, extract
, move
, quick_append
124 get_pos_bfd (bfd
**, enum pos
, const char *);
126 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
127 extract the COUNTED_NAME_COUNTER instance of that name. */
128 static bfd_boolean counted_name_mode
= 0;
129 static int counted_name_counter
= 0;
131 /* Whether to truncate names of files stored in the archive. */
132 static bfd_boolean ar_truncate
= FALSE
;
134 /* Whether to use a full file name match when searching an archive.
135 This is convenient for archives created by the Microsoft lib
137 static bfd_boolean full_pathname
= FALSE
;
139 /* Whether to create a "thin" archive (symbol index only -- no files). */
140 static bfd_boolean make_thin_archive
= FALSE
;
142 #define LIBDEPS "__.LIBDEP"
143 /* Text to store in the __.LIBDEP archive element for the linker to use. */
144 static char * libdeps
= NULL
;
145 static bfd
* libdeps_bfd
= NULL
;
147 static int show_version
= 0;
149 static int show_help
= 0;
151 #if BFD_SUPPORTS_PLUGINS
152 static const char *plugin_target
= "plugin";
154 static const char *plugin_target
= NULL
;
157 static const char *target
= NULL
;
159 enum long_option_numbers
166 static const char * output_dir
= NULL
;
168 static struct option long_options
[] =
170 {"help", no_argument
, &show_help
, 1},
171 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
172 {"target", required_argument
, NULL
, OPTION_TARGET
},
173 {"version", no_argument
, &show_version
, 1},
174 {"output", required_argument
, NULL
, OPTION_OUTPUT
},
175 {"record-libdeps", required_argument
, NULL
, 'l'},
176 {NULL
, no_argument
, NULL
, 0}
184 interactive
= isatty (fileno (stdin
));
188 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
189 COUNT is the length of the FILES chain; FUNCTION is called on each entry
190 whose name matches one in FILES. */
193 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
200 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
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
214 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
215 head
->archive_pass
= 0;
217 for (; count
> 0; files
++, count
--)
219 bfd_boolean found
= FALSE
;
222 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
224 const char * filename
;
227 /* PR binutils/15796: Once an archive element has been matched
228 do not match it again. If the user provides multiple same-named
229 parameters on the command line their intent is to match multiple
230 same-named entries in the archive, not the same entry multiple
232 if (head
->archive_pass
)
235 filename
= bfd_get_filename (head
);
236 if (filename
== NULL
)
238 /* Some archive formats don't get the filenames filled in
239 until the elements are opened. */
241 bfd_stat_arch_elt (head
, &buf
);
243 else if (bfd_is_thin_archive (arch
))
245 /* Thin archives store full pathnames. Need to normalize. */
246 filename
= normalize (filename
, arch
);
250 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
253 if (counted_name_mode
254 && match_count
!= counted_name_counter
)
256 /* Counting, and didn't match on count; go on to the
263 head
->archive_pass
= 1;
264 /* PR binutils/15796: Once a file has been matched, do not
265 match any more same-named files in the archive. If the
266 user does want to match multiple same-name files in an
267 archive they should provide multiple same-name parameters
268 to the ar command. */
274 /* xgettext:c-format */
275 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
279 bfd_boolean operation_alters_arch
= FALSE
;
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");
293 /* xgettext:c-format */
294 const char *command_line
295 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
296 " [member-name] [count] archive-file file...\n");
298 s
= help
? stdout
: stderr
;
300 fprintf (s
, command_line
, program_name
);
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
)
319 [D] - use zero for timestamps and uids/gids (default)\n"));
321 [U] - use actual timestamps and uids/gids\n"));
326 [D] - use zero for timestamps and uids/gids\n"));
328 [U] - use actual timestamps and uids/gids (default)\n"));
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] - make a thin archive\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 #if BFD_SUPPORTS_PLUGINS
349 fprintf (s
, _(" optional:\n"));
350 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
355 list_supported_targets (program_name
, s
);
357 if (REPORT_BUGS_TO
[0] && help
)
358 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
360 xexit (help
? 0 : 1);
364 ranlib_usage (int help
)
368 s
= help
? stdout
: stderr
;
370 /* xgettext:c-format */
371 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
372 fprintf (s
, _(" Generate an index to speed access to archives\n"));
373 fprintf (s
, _(" The options are:\n\
374 @<file> Read options from <file>\n"));
375 #if BFD_SUPPORTS_PLUGINS
377 --plugin <name> Load the specified plugin\n"));
379 if (DEFAULT_AR_DETERMINISTIC
)
381 -D Use zero for symbol map timestamp (default)\n\
382 -U Use an actual symbol map timestamp\n"));
385 -D Use zero for symbol map timestamp\n\
386 -U Use actual symbol map timestamp (default)\n"));
388 -t Update the archive's symbol map timestamp\n\
389 -h --help Print this help message\n\
390 -v --version Print version information\n"));
392 list_supported_targets (program_name
, s
);
394 if (REPORT_BUGS_TO
[0] && help
)
395 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
397 xexit (help
? 0 : 1);
400 /* Normalize a file name specified on the command line into a file
401 name which we will use in an archive. */
404 normalize (const char *file
, bfd
*abfd
)
406 const char *filename
;
411 filename
= lbasename (file
);
415 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
420 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
421 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
422 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
429 /* Remove any output file. This is only called via xatexit. */
431 static const char *output_filename
= NULL
;
432 static FILE *output_file
= NULL
;
433 static bfd
*output_bfd
= NULL
;
438 if (output_filename
!= NULL
)
440 if (output_bfd
!= NULL
)
441 bfd_cache_close (output_bfd
);
442 if (output_file
!= NULL
)
443 fclose (output_file
);
444 unlink_if_ordinary (output_filename
);
449 decode_options (int argc
, char **argv
)
453 /* Convert old-style ar call by exploding option element and rearranging
454 options accordingly. */
457 if (argc
> 1 && argv
[1][0] != '-')
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 */
466 /* Initialize a constructed option. */
471 /* Allocate a new argument array, and copy program name in it. */
473 new_argc
= argc
- 1 + strlen (argv
[1]);
474 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
479 /* Copy each old letter option as a separate option. */
481 for (letter
= *in
++; *letter
; letter
++)
484 *out
++ = xstrdup (buffer
);
487 /* Copy all remaining options. */
489 while (in
< argv
+ argc
)
493 /* Replace the old option list by the new one. */
499 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxl:coOVsSuvabiMNfPTDU",
500 long_options
, NULL
)) != EOF
)
511 if (operation
!= none
)
512 fatal (_("two different operation options specified"));
523 operation_alters_arch
= TRUE
;
527 operation_alters_arch
= TRUE
;
530 operation
= print_files
;
533 operation
= quick_append
;
534 operation_alters_arch
= TRUE
;
538 operation_alters_arch
= TRUE
;
541 operation
= print_table
;
548 fatal (_("libdeps specified more than once"));
579 postype
= pos_before
;
582 postype
= pos_before
;
588 counted_name_mode
= TRUE
;
594 full_pathname
= TRUE
;
597 make_thin_archive
= TRUE
;
600 deterministic
= TRUE
;
603 deterministic
= FALSE
;
606 #if BFD_SUPPORTS_PLUGINS
607 bfd_plugin_set_plugin (optarg
);
609 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
619 case 0: /* A long option that just sets a flag. */
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
)
632 argv
+= (optind
- 1);
633 argc
-= (optind
- 1);
638 return &argv
[optind
];
641 /* If neither -D nor -U was specified explicitly,
642 then use the configured default. */
644 default_deterministic (void)
646 if (deterministic
< 0)
647 deterministic
= DEFAULT_AR_DETERMINISTIC
;
651 ranlib_main (int argc
, char **argv
)
653 int arg_index
, status
= 0;
654 bfd_boolean touch
= FALSE
;
657 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
662 deterministic
= TRUE
;
665 deterministic
= FALSE
;
679 /* PR binutils/13493: Support plugins. */
681 #if BFD_SUPPORTS_PLUGINS
682 bfd_plugin_set_plugin (optarg
);
684 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
698 print_version ("ranlib");
700 default_deterministic ();
704 while (arg_index
< argc
)
707 status
|= ranlib_only (argv
[arg_index
]);
709 status
|= ranlib_touch (argv
[arg_index
]);
716 int main (int, char **);
719 main (int argc
, char **argv
)
724 char *inarch_filename
;
727 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
728 setlocale (LC_MESSAGES
, "");
730 #if defined (HAVE_SETLOCALE)
731 setlocale (LC_CTYPE
, "");
733 bindtextdomain (PACKAGE
, LOCALEDIR
);
734 textdomain (PACKAGE
);
736 program_name
= argv
[0];
737 xmalloc_set_program_name (program_name
);
738 bfd_set_error_program_name (program_name
);
739 #if BFD_SUPPORTS_PLUGINS
740 bfd_plugin_set_program_name (program_name
);
743 expandargv (&argc
, &argv
);
747 const char *temp
= lbasename (program_name
);
749 if (strlen (temp
) >= 6
750 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
756 START_PROGRESS (program_name
, 0);
758 if (bfd_init () != BFD_INIT_MAGIC
)
759 fatal (_("fatal error: libbfd ABI mismatch"));
760 set_default_bfd_target ();
762 xatexit (remove_output
);
764 for (i
= 1; i
< argc
; i
++)
765 if (! ar_emul_parse_arg (argv
[i
]))
771 ranlib_main (argc
, argv
);
776 argv
= decode_options (argc
, argv
);
782 print_version ("ar");
788 default_deterministic ();
795 /* Fail if no files are specified on the command line.
796 (But not for MRI mode which allows for reading arguments
797 and filenames from stdin). */
798 if (argv
[arg_index
] == NULL
)
801 /* We don't use do_quick_append any more. Too many systems
802 expect ar to always rebuild the symbol table even when q is
805 /* We can't write an armap when using ar q, so just do ar r
807 if (operation
== quick_append
&& write_armap
)
810 if ((operation
== none
|| operation
== print_table
)
812 xexit (ranlib_only (argv
[arg_index
]));
814 if (operation
== none
)
815 fatal (_("no operation specified"));
817 if (newer_only
&& operation
!= replace
)
818 fatal (_("`u' is only meaningful with the `r' option."));
820 if (newer_only
&& deterministic
> 0)
821 fatal (_("`u' is not meaningful with the `D' option."));
823 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
825 `u' modifier ignored since `D' is the default (see `U')"));
827 default_deterministic ();
829 if (postype
!= pos_default
)
831 posname
= argv
[arg_index
++];
833 fatal (_("missing position arg."));
836 if (counted_name_mode
)
838 if (operation
!= extract
&& operation
!= del
)
839 fatal (_("`N' is only meaningful with the `x' and `d' options."));
840 if (argv
[arg_index
] == NULL
)
841 fatal (_("`N' missing value."));
842 counted_name_counter
= atoi (argv
[arg_index
++]);
843 if (counted_name_counter
<= 0)
844 fatal (_("Value for `N' must be positive."));
847 inarch_filename
= argv
[arg_index
++];
848 if (inarch_filename
== NULL
)
851 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
854 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
856 arch
= open_inarch (inarch_filename
,
857 files
== NULL
? (char *) NULL
: files
[0]);
859 if (operation
== extract
&& bfd_is_thin_archive (arch
))
860 fatal (_("`x' cannot be used on thin archives."));
865 bfd_size_type reclen
= strlen (libdeps
) + 1;
867 /* Create a bfd to contain the dependencies.
868 It inherits its type from arch, but we must set the type to
869 "binary" otherwise bfd_bwrite() will fail. After writing, we
870 must set the type back to default otherwise adding it to the
871 archive will fail. */
872 libdeps_bfd
= bfd_create (LIBDEPS
, arch
);
873 if (libdeps_bfd
== NULL
)
874 fatal (_("Cannot create libdeps record."));
876 if (bfd_find_target ("binary", libdeps_bfd
) == NULL
)
877 fatal (_("Cannot set libdeps record type to binary."));
879 if (! bfd_set_format (libdeps_bfd
, bfd_object
))
880 fatal (_("Cannot set libdeps object format."));
882 if (! bfd_make_writable (libdeps_bfd
))
883 fatal (_("Cannot make libdeps object writable."));
885 if (bfd_bwrite (libdeps
, reclen
, libdeps_bfd
) != reclen
)
886 fatal (_("Cannot write libdeps record."));
888 if (! bfd_make_readable (libdeps_bfd
))
889 fatal (_("Cannot make libdeps object readable."));
891 if (bfd_find_target (plugin_target
, libdeps_bfd
) == NULL
)
892 fatal (_("Cannot reset libdeps record type."));
894 /* Insert our libdeps record in 2nd slot of the list of files
895 being operated on. We shouldn't use 1st slot, but we want
896 to avoid having to search all the way to the end of an
897 archive with a large number of members at link time. */
898 new_files
= xmalloc ((file_count
+ 2) * sizeof (char *));
899 new_files
[0] = files
[0];
900 new_files
[1] = LIBDEPS
;
901 for (i
= 1; i
< file_count
; i
++)
902 new_files
[i
+1] = files
[i
];
911 map_over_members (arch
, print_descr
, files
, file_count
);
915 map_over_members (arch
, print_contents
, files
, file_count
);
919 map_over_members (arch
, extract_file
, files
, file_count
);
924 delete_members (arch
, files
);
926 output_filename
= NULL
;
930 /* PR 12558: Creating and moving at the same time does
931 not make sense. Just create the archive instead. */
935 move_members (arch
, files
);
937 output_filename
= NULL
;
944 if (files
!= NULL
|| write_armap
> 0)
945 replace_members (arch
, files
, operation
== quick_append
);
947 output_filename
= NULL
;
950 /* Shouldn't happen! */
952 /* xgettext:c-format */
953 fatal (_("internal error -- this option not implemented"));
957 END_PROGRESS (program_name
);
964 open_inarch (const char *archive_filename
, const char *file
)
972 bfd_set_error (bfd_error_no_error
);
975 target
= plugin_target
;
977 if (stat (archive_filename
, &sbuf
) != 0)
979 #if !defined(__GO32__) || defined(__DJGPP__)
981 /* FIXME: I don't understand why this fragment was ifndef'ed
982 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
983 stat() works just fine in v2.x, so I think this should be
984 removed. For now, I enable it for DJGPP v2. -- EZ. */
986 /* KLUDGE ALERT! Temporary fix until I figger why
987 stat() is wrong ... think it's buried in GO32's IDT - Jax */
989 bfd_fatal (archive_filename
);
992 if (!operation_alters_arch
)
994 fprintf (stderr
, "%s: ", program_name
);
995 perror (archive_filename
);
1000 /* If the target isn't set, try to figure out the target to use
1001 for the archive from the first object on the list. */
1002 if (target
== NULL
&& file
!= NULL
)
1006 obj
= bfd_openr (file
, target
);
1009 if (bfd_check_format (obj
, bfd_object
))
1010 target
= bfd_get_target (obj
);
1011 (void) bfd_close (obj
);
1015 /* Create an empty archive. */
1016 arch
= bfd_openw (archive_filename
, target
);
1018 || ! bfd_set_format (arch
, bfd_archive
)
1019 || ! bfd_close (arch
))
1020 bfd_fatal (archive_filename
);
1021 else if (!silent_create
)
1022 non_fatal (_("creating %s"), archive_filename
);
1024 /* If we die creating a new archive, don't leave it around. */
1025 output_filename
= archive_filename
;
1028 arch
= bfd_openr (archive_filename
, target
);
1032 bfd_fatal (archive_filename
);
1035 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1037 bfd_nonfatal (archive_filename
);
1038 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1040 list_matching_formats (matching
);
1046 if ((operation
== replace
|| operation
== quick_append
)
1047 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
1049 /* PR 15140: Catch attempts to convert a normal
1050 archive into a thin archive or vice versa. */
1051 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
1053 fatal (_("Cannot convert existing library %s to thin format"),
1054 bfd_get_filename (arch
));
1057 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
1059 fatal (_("Cannot convert existing thin library %s to normal format"),
1060 bfd_get_filename (arch
));
1065 last_one
= &(arch
->archive_next
);
1066 /* Read all the contents right away, regardless. */
1067 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
1069 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
1072 *last_one
= next_one
;
1073 last_one
= &next_one
->archive_next
;
1075 *last_one
= (bfd
*) NULL
;
1076 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1082 print_contents (bfd
*abfd
)
1084 bfd_size_type ncopied
= 0;
1086 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1089 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1090 /* xgettext:c-format */
1091 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1094 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
1096 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1099 while (ncopied
< size
)
1101 bfd_size_type nread
;
1102 bfd_size_type tocopy
= size
- ncopied
;
1104 if (tocopy
> BUFSIZE
)
1107 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1108 if (nread
!= tocopy
)
1109 /* xgettext:c-format */
1110 fatal (_("%s is not a valid archive"),
1111 bfd_get_filename (abfd
->my_archive
));
1113 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1114 return value to bfd_size_type to avoid comparison between signed and
1116 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1117 fatal ("stdout: %s", strerror (errno
));
1124 static FILE * open_output_file (bfd
*) ATTRIBUTE_RETURNS_NONNULL
;
1127 open_output_file (bfd
* abfd
)
1129 output_filename
= bfd_get_filename (abfd
);
1131 /* PR binutils/17533: Do not allow directory traversal
1132 outside of the current directory tree - unless the
1133 user has explicitly specified an output directory. */
1134 if (! is_valid_archive_path (output_filename
))
1136 char * base
= (char *) lbasename (output_filename
);
1138 non_fatal (_("illegal output pathname for archive member: %s, using '%s' instead"),
1139 output_filename
, base
);
1140 output_filename
= base
;
1145 size_t len
= strlen (output_dir
);
1149 /* FIXME: There is a memory leak here, but it is not serious. */
1150 if (IS_DIR_SEPARATOR (output_dir
[len
- 1]))
1151 output_filename
= concat (output_dir
, output_filename
, NULL
);
1153 output_filename
= concat (output_dir
, "/", output_filename
, NULL
);
1158 printf ("x - %s\n", output_filename
);
1160 FILE * ostream
= fopen (output_filename
, FOPEN_WB
);
1161 if (ostream
== NULL
)
1163 perror (output_filename
);
1170 /* Extract a member of the archive into its own file.
1172 We defer opening the new file until after we have read a BUFSIZ chunk of the
1173 old one, since we know we have just read the archive header for the old
1174 one. Since most members are shorter than BUFSIZ, this means we will read
1175 the old header, read the old data, write a new inode for the new file, and
1176 write the new data, and be done. This 'optimization' is what comes from
1177 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1181 extract_file (bfd
*abfd
)
1186 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1187 /* xgettext:c-format */
1188 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1191 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1196 output_file
= open_output_file (abfd
);
1200 bfd_size_type ncopied
= 0;
1201 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1203 while (ncopied
< size
)
1205 bfd_size_type nread
, tocopy
;
1207 tocopy
= size
- ncopied
;
1208 if (tocopy
> BUFSIZE
)
1211 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1212 if (nread
!= tocopy
)
1213 /* xgettext:c-format */
1214 fatal (_("%s is not a valid archive"),
1215 bfd_get_filename (abfd
->my_archive
));
1217 /* See comment above; this saves disk arm motion. */
1218 if (output_file
== NULL
)
1219 output_file
= open_output_file (abfd
);
1221 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1222 the return value to bfd_size_type to avoid comparison between
1223 signed and unsigned values. */
1224 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, output_file
) != nread
)
1225 fatal ("%s: %s", output_filename
, strerror (errno
));
1233 fclose (output_file
);
1237 chmod (output_filename
, buf
.st_mode
);
1241 /* Set access time to modification time. Only st_mtime is
1242 initialized by bfd_stat_arch_elt. */
1243 buf
.st_atime
= buf
.st_mtime
;
1244 set_times (output_filename
, &buf
);
1247 output_filename
= NULL
;
1251 write_archive (bfd
*iarch
)
1254 char *old_name
, *new_name
;
1255 bfd
*contents_head
= iarch
->archive_next
;
1257 struct stat target_stat
;
1258 bfd_boolean skip_stat
= FALSE
;
1260 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1261 strcpy (old_name
, bfd_get_filename (iarch
));
1262 new_name
= make_tempname (old_name
, &ofd
);
1264 if (new_name
== NULL
)
1265 bfd_fatal (_("could not create temporary file whilst writing archive"));
1267 output_filename
= new_name
;
1269 obfd
= bfd_fdopenw (new_name
, bfd_get_target (iarch
), ofd
);
1274 bfd_fatal (old_name
);
1279 bfd_set_format (obfd
, bfd_archive
);
1281 /* Request writing the archive symbol table unless we've
1282 been explicitly requested not to. */
1283 obfd
->has_armap
= write_armap
>= 0;
1287 /* This should really use bfd_set_file_flags, but that rejects
1289 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1293 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1296 obfd
->flags
|= BFD_ARCHIVE_FULL_PATH
;
1298 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1299 bfd_set_thin_archive (obfd
, TRUE
);
1301 if (!bfd_set_archive_head (obfd
, contents_head
))
1302 bfd_fatal (old_name
);
1304 #if !defined (_WIN32) || defined (__CYGWIN32__)
1306 if (iarch
== NULL
|| iarch
->iostream
== NULL
)
1308 else if (ofd
== -1 || fstat (fileno ((FILE *) iarch
->iostream
), &target_stat
) != 0)
1309 bfd_fatal (old_name
);
1312 if (!bfd_close (obfd
))
1313 bfd_fatal (old_name
);
1316 output_filename
= NULL
;
1318 /* We don't care if this fails; we might be creating the archive. */
1321 if (smart_rename (new_name
, old_name
, ofd
, skip_stat
? NULL
: &target_stat
, 0) != 0)
1327 /* Return a pointer to the pointer to the entry which should be rplacd'd
1328 into when altering. DEFAULT_POS should be how to interpret pos_default,
1329 and should be a pos value. */
1332 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1334 bfd
**after_bfd
= contents
;
1336 const char *realposname
;
1338 if (postype
== pos_default
)
1340 realpos
= default_pos
;
1341 realposname
= default_posname
;
1346 realposname
= posname
;
1349 if (realpos
== pos_end
)
1352 after_bfd
= &((*after_bfd
)->archive_next
);
1356 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1357 if (FILENAME_CMP (bfd_get_filename (*after_bfd
), realposname
) == 0)
1359 if (realpos
== pos_after
)
1360 after_bfd
= &(*after_bfd
)->archive_next
;
1368 delete_members (bfd
*arch
, char **files_to_delete
)
1370 bfd
**current_ptr_ptr
;
1372 bfd_boolean something_changed
= FALSE
;
1375 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1377 /* In a.out systems, the armap is optional. It's also called
1378 __.SYMDEF. So if the user asked to delete it, we should remember
1379 that fact. This isn't quite right for COFF systems (where
1380 __.SYMDEF might be regular member), but it's very unlikely
1381 to be a problem. FIXME */
1383 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1385 arch
->has_armap
= FALSE
;
1392 current_ptr_ptr
= &(arch
->archive_next
);
1393 while (*current_ptr_ptr
)
1395 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1396 bfd_get_filename (*current_ptr_ptr
)) == 0)
1399 if (counted_name_mode
1400 && match_count
!= counted_name_counter
)
1402 /* Counting, and didn't match on count; go on to the
1408 something_changed
= TRUE
;
1412 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1417 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1420 if (verbose
&& !found
)
1422 /* xgettext:c-format */
1423 printf (_("No member named `%s'\n"), *files_to_delete
);
1429 if (something_changed
)
1430 write_archive (arch
);
1432 output_filename
= NULL
;
1436 /* Reposition existing members within an archive */
1439 move_members (bfd
*arch
, char **files_to_move
)
1441 bfd
**after_bfd
; /* New entries go after this one */
1442 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1444 for (; *files_to_move
; ++files_to_move
)
1446 current_ptr_ptr
= &(arch
->archive_next
);
1447 while (*current_ptr_ptr
)
1449 bfd
*current_ptr
= *current_ptr_ptr
;
1450 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1451 bfd_get_filename (current_ptr
)) == 0)
1453 /* Move this file to the end of the list - first cut from
1456 *current_ptr_ptr
= current_ptr
->archive_next
;
1458 /* Now glue to end */
1459 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1460 link_bfd
= *after_bfd
;
1461 *after_bfd
= current_ptr
;
1462 current_ptr
->archive_next
= link_bfd
;
1465 printf ("m - %s\n", *files_to_move
);
1470 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1472 /* xgettext:c-format */
1473 fatal (_("no entry %s in archive %s!"), *files_to_move
,
1474 bfd_get_filename (arch
));
1479 write_archive (arch
);
1482 /* Ought to default to replacing in place, but this is existing practice! */
1485 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1487 bfd_boolean changed
= FALSE
;
1488 bfd
**after_bfd
; /* New entries go after this one. */
1492 while (files_to_move
&& *files_to_move
)
1496 current_ptr
= &arch
->archive_next
;
1497 while (*current_ptr
)
1499 current
= *current_ptr
;
1501 /* For compatibility with existing ar programs, we
1502 permit the same file to be added multiple times. */
1503 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1504 normalize (bfd_get_filename (current
), arch
)) == 0
1505 && current
->arelt_data
!= NULL
)
1507 bfd_boolean replaced
;
1510 struct stat fsbuf
, asbuf
;
1512 if (stat (*files_to_move
, &fsbuf
) != 0)
1514 if (errno
!= ENOENT
)
1515 bfd_fatal (*files_to_move
);
1518 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1519 /* xgettext:c-format */
1520 fatal (_("internal stat error on %s"),
1521 bfd_get_filename (current
));
1523 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1527 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1528 bfd_get_filename (current
));
1529 if (libdeps_bfd
!= NULL
1530 && FILENAME_CMP (normalize (*files_to_move
, arch
),
1533 replaced
= ar_emul_replace_bfd (after_bfd
, libdeps_bfd
,
1538 replaced
= ar_emul_replace (after_bfd
, *files_to_move
,
1543 /* Snip out this entry from the chain. */
1544 *current_ptr
= (*current_ptr
)->archive_next
;
1550 current_ptr
= &(current
->archive_next
);
1554 /* Add to the end of the archive. */
1555 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1557 if (libdeps_bfd
!= NULL
1558 && FILENAME_CMP (normalize (*files_to_move
, arch
), LIBDEPS
) == 0)
1560 changed
|= ar_emul_append_bfd (after_bfd
, libdeps_bfd
,
1561 verbose
, make_thin_archive
);
1565 changed
|= ar_emul_append (after_bfd
, *files_to_move
, target
,
1566 verbose
, make_thin_archive
);
1575 write_archive (arch
);
1577 output_filename
= NULL
;
1581 ranlib_only (const char *archname
)
1585 if (get_file_size (archname
) < 1)
1588 arch
= open_inarch (archname
, (char *) NULL
);
1591 write_archive (arch
);
1595 /* Update the timestamp of the symbol map of an archive. */
1598 ranlib_touch (const char *archname
)
1601 /* I don't think updating works on go32. */
1602 ranlib_only (archname
);
1608 if (get_file_size (archname
) < 1)
1610 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1613 bfd_set_error (bfd_error_system_call
);
1614 bfd_fatal (archname
);
1617 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1619 bfd_fatal (archname
);
1620 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1622 bfd_nonfatal (archname
);
1623 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1625 list_matching_formats (matching
);
1631 if (! bfd_has_map (arch
))
1632 /* xgettext:c-format */
1633 fatal (_("%s: no archive map to update"), archname
);
1636 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1638 bfd_update_armap_timestamp (arch
);
1640 if (! bfd_close (arch
))
1641 bfd_fatal (archname
);
1646 /* Things which are interesting to map over all or some of the files: */
1649 print_descr (bfd
*abfd
)
1651 print_arelt_descr (stdout
, abfd
, verbose
, display_offsets
);