1 /* ar.c - Archive modify and extract.
2 Copyright (C) 1991-2025 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. */
28 #include "libiberty.h"
33 #include "filenames.h"
35 #include "plugin-api.h"
40 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
42 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
45 /* Static declarations. */
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
);
54 static void move_members (bfd
*, char **files_to_move
);
55 static void replace_members
56 (bfd
*, char **files_to_replace
, bool 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);
63 /** Globals and flags. */
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. */
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 bool counted_name_mode
= 0;
129 static int counted_name_counter
= 0;
131 /* Whether to truncate names of files stored in the archive. */
132 static bool 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 bool full_pathname
= false;
139 /* Whether to create a "thin" archive (symbol index only -- no files). */
140 static bool 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 {"thin", no_argument
, NULL
, 'T'},
177 {NULL
, no_argument
, NULL
, 0}
185 interactive
= isatty (fileno (stdin
));
189 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
190 COUNT is the length of the FILES chain; FUNCTION is called on each entry
191 whose name matches one in FILES. */
194 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
201 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
206 /* This may appear to be a baroque way of accomplishing what we want.
207 However we have to iterate over the filenames in order to notice where
208 a filename is requested but does not exist in the archive. Ditto
209 mapping over each file each time -- we want to hack multiple
212 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
213 head
->archive_pass
= 0;
215 for (; count
> 0; files
++, count
--)
220 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
222 const char * filename
;
224 /* PR binutils/15796: Once an archive element has been matched
225 do not match it again. If the user provides multiple same-named
226 parameters on the command line their intent is to match multiple
227 same-named entries in the archive, not the same entry multiple
229 if (head
->archive_pass
)
232 filename
= bfd_get_filename (head
);
233 if (filename
== NULL
)
235 /* Some archive formats don't get the filenames filled in
236 until the elements are opened. */
238 bfd_stat_arch_elt (head
, &buf
);
240 else if (bfd_is_thin_archive (arch
))
242 /* Thin archives store full pathnames. Need to normalize. */
243 filename
= normalize (filename
, arch
);
247 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
250 if (counted_name_mode
251 && match_count
!= counted_name_counter
)
253 /* Counting, and didn't match on count; go on to the
260 head
->archive_pass
= 1;
261 /* PR binutils/15796: Once a file has been matched, do not
262 match any more same-named files in the archive. If the
263 user does want to match multiple same-name files in an
264 archive they should provide multiple same-name parameters
265 to the ar command. */
271 /* xgettext:c-format */
272 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
276 bool operation_alters_arch
= false;
283 #if BFD_SUPPORTS_PLUGINS
284 /* xgettext:c-format */
285 const char *command_line
286 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
287 " [--plugin <name>] [member-name] [count] archive-file file...\n");
290 /* xgettext:c-format */
291 const char *command_line
292 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
293 " [member-name] [count] archive-file file...\n");
295 s
= help
? stdout
: stderr
;
297 fprintf (s
, command_line
, program_name
);
299 /* xgettext:c-format */
300 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
301 fprintf (s
, _(" commands:\n"));
302 fprintf (s
, _(" d - delete file(s) from the archive\n"));
303 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
304 fprintf (s
, _(" p - print file(s) found in the archive\n"));
305 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
306 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
307 fprintf (s
, _(" s - act as ranlib\n"));
308 fprintf (s
, _(" t[O][v] - display contents of the archive\n"));
309 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
310 fprintf (s
, _(" command specific modifiers:\n"));
311 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
312 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
313 if (DEFAULT_AR_DETERMINISTIC
)
316 [D] - use zero for timestamps and uids/gids (default)\n"));
318 [U] - use actual timestamps and uids/gids\n"));
323 [D] - use zero for timestamps and uids/gids\n"));
325 [U] - use actual timestamps and uids/gids (default)\n"));
327 fprintf (s
, _(" [N] - use instance [count] of name\n"));
328 fprintf (s
, _(" [f] - truncate inserted file names\n"));
329 fprintf (s
, _(" [P] - use full path names when matching\n"));
330 fprintf (s
, _(" [o] - preserve original dates\n"));
331 fprintf (s
, _(" [O] - display offsets of files in the archive\n"));
332 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
333 fprintf (s
, _(" generic modifiers:\n"));
334 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
335 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
336 fprintf (s
, _(" [l <text> ] - specify the dependencies of this library\n"));
337 fprintf (s
, _(" [S] - do not build a symbol table\n"));
338 fprintf (s
, _(" [T] - deprecated, use --thin instead\n"));
339 fprintf (s
, _(" [v] - be verbose\n"));
340 fprintf (s
, _(" [V] - display the version number\n"));
341 fprintf (s
, _(" @<file> - read options from <file>\n"));
342 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
343 fprintf (s
, _(" --output=DIRNAME - specify the output directory for extraction operations\n"));
344 fprintf (s
, _(" --record-libdeps=<text> - specify the dependencies of this library\n"));
345 fprintf (s
, _(" --thin - make a thin archive\n"));
346 #if BFD_SUPPORTS_PLUGINS
347 fprintf (s
, _(" optional:\n"));
348 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
353 list_supported_targets (program_name
, s
);
355 if (REPORT_BUGS_TO
[0] && help
)
356 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
358 xexit (help
? 0 : 1);
362 ranlib_usage (int help
)
366 s
= help
? stdout
: stderr
;
368 /* xgettext:c-format */
369 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
370 fprintf (s
, _(" Generate an index to speed access to archives\n"));
371 fprintf (s
, _(" The options are:\n\
372 @<file> Read options from <file>\n"));
373 #if BFD_SUPPORTS_PLUGINS
375 --plugin <name> Load the specified plugin\n"));
377 if (DEFAULT_AR_DETERMINISTIC
)
379 -D Use zero for symbol map timestamp (default)\n\
380 -U Use an actual symbol map timestamp\n"));
383 -D Use zero for symbol map timestamp\n\
384 -U Use actual symbol map timestamp (default)\n"));
386 -t Update the archive's symbol map timestamp\n\
387 -h --help Print this help message\n\
388 -v --version Print version information\n"));
390 list_supported_targets (program_name
, s
);
392 if (REPORT_BUGS_TO
[0] && help
)
393 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
395 xexit (help
? 0 : 1);
398 /* Normalize a file name specified on the command line into a file
399 name which we will use in an archive. */
402 normalize (const char *file
, bfd
*abfd
)
404 const char *filename
;
409 filename
= lbasename (file
);
413 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
418 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
419 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
420 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
427 /* Remove any output file. This is only called via xatexit. */
429 static char *output_filename
= NULL
;
430 static FILE *output_file
= NULL
;
435 if (output_filename
!= NULL
)
437 if (output_file
!= NULL
)
438 fclose (output_file
);
439 unlink_if_ordinary (output_filename
);
440 free (output_filename
);
441 output_filename
= NULL
;
446 decode_options (int argc
, char **argv
)
450 /* Convert old-style ar call by exploding option element and rearranging
451 options accordingly. */
454 if (argc
> 1 && argv
[1][0] != '-')
456 int new_argc
; /* argc value for rearranged arguments */
457 char **new_argv
; /* argv value for rearranged arguments */
458 char *const *in
; /* cursor into original argv */
459 char **out
; /* cursor into rearranged argv */
460 const char *letter
; /* cursor into old option letters */
461 char buffer
[3]; /* constructed option buffer */
463 /* Initialize a constructed option. */
468 /* Allocate a new argument array, and copy program name in it. */
470 new_argc
= argc
- 1 + strlen (argv
[1]);
471 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
476 /* Copy each old letter option as a separate option. */
478 for (letter
= *in
++; *letter
; letter
++)
481 *out
++ = xstrdup (buffer
);
484 /* Copy all remaining options. */
486 while (in
< argv
+ argc
)
490 /* Replace the old option list by the new one. */
496 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxl:coOVsSuvabiMNfPTDU",
497 long_options
, NULL
)) != EOF
)
508 if (operation
!= none
)
509 fatal (_("two different operation options specified"));
520 operation_alters_arch
= true;
524 operation_alters_arch
= true;
527 operation
= print_files
;
530 operation
= quick_append
;
531 operation_alters_arch
= true;
535 operation_alters_arch
= true;
538 operation
= print_table
;
545 fatal (_("libdeps specified more than once"));
576 postype
= pos_before
;
579 postype
= pos_before
;
585 counted_name_mode
= true;
591 full_pathname
= true;
594 make_thin_archive
= true;
597 deterministic
= true;
600 deterministic
= false;
603 #if BFD_SUPPORTS_PLUGINS
604 bfd_plugin_set_plugin (optarg
);
606 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
616 case 0: /* A long option that just sets a flag. */
623 /* PR 13256: Allow for the possibility that the first command line option
624 started with a dash (eg --plugin) but then the following option(s) are
625 old style, non-dash-prefixed versions. */
626 if (operation
== none
&& write_armap
!= 1 && !mri_mode
627 && optind
> 0 && optind
< argc
)
629 argv
+= (optind
- 1);
630 argc
-= (optind
- 1);
635 return &argv
[optind
];
638 /* If neither -D nor -U was specified explicitly,
639 then use the configured default. */
641 default_deterministic (void)
643 if (deterministic
< 0)
644 deterministic
= DEFAULT_AR_DETERMINISTIC
;
648 ranlib_main (int argc
, char **argv
)
650 int arg_index
, status
= 0;
654 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
659 deterministic
= true;
662 deterministic
= false;
676 /* PR binutils/13493: Support plugins. */
678 #if BFD_SUPPORTS_PLUGINS
679 bfd_plugin_set_plugin (optarg
);
681 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
695 print_version ("ranlib");
697 default_deterministic ();
701 while (arg_index
< argc
)
704 status
|= ranlib_only (argv
[arg_index
]);
706 status
|= ranlib_touch (argv
[arg_index
]);
713 int main (int, char **);
716 main (int argc
, char **argv
)
721 char *inarch_filename
;
724 #ifdef HAVE_LC_MESSAGES
725 setlocale (LC_MESSAGES
, "");
727 setlocale (LC_CTYPE
, "");
728 bindtextdomain (PACKAGE
, LOCALEDIR
);
729 textdomain (PACKAGE
);
731 program_name
= argv
[0];
732 xmalloc_set_program_name (program_name
);
733 bfd_set_error_program_name (program_name
);
734 #if BFD_SUPPORTS_PLUGINS
735 bfd_plugin_set_program_name (program_name
);
738 expandargv (&argc
, &argv
);
743 size_t l
= strlen (program_name
);
745 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
746 /* Drop the .exe suffix, if any. */
747 if (l
> 4 && FILENAME_CMP (program_name
+ l
- 4, ".exe") == 0)
750 program_name
[l
] = '\0';
753 is_ranlib
= (l
>= 6 &&
754 FILENAME_CMP (program_name
+ l
- 6, "ranlib") == 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 non_fatal (_("`u' is not meaningful with the `D' option - replacement will always happen."));
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_write() 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_write (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 (*new_files
));
901 new_files
[0] = files
[0];
902 memcpy (new_files
+ 1, files
, file_count
* sizeof (*files
));
904 new_files
[file_count
!= 0] = LIBDEPS
;
906 new_files
[file_count
] = NULL
;
913 map_over_members (arch
, print_descr
, files
, file_count
);
917 map_over_members (arch
, print_contents
, files
, file_count
);
921 map_over_members (arch
, extract_file
, files
, file_count
);
926 delete_members (arch
, files
);
929 free (output_filename
);
930 output_filename
= NULL
;
935 /* PR 12558: Creating and moving at the same time does
936 not make sense. Just create the archive instead. */
940 move_members (arch
, files
);
943 free (output_filename
);
944 output_filename
= NULL
;
952 if (files
!= NULL
|| write_armap
> 0)
953 replace_members (arch
, files
, operation
== quick_append
);
956 free (output_filename
);
957 output_filename
= NULL
;
961 /* Shouldn't happen! */
963 /* xgettext:c-format */
964 fatal (_("internal error -- this option not implemented"));
973 open_inarch (const char *archive_filename
, const char *file
)
981 bfd_set_error (bfd_error_no_error
);
984 target
= plugin_target
;
986 if (stat (archive_filename
, &sbuf
) != 0)
988 #if !defined(__GO32__) || defined(__DJGPP__)
990 /* FIXME: I don't understand why this fragment was ifndef'ed
991 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
992 stat() works just fine in v2.x, so I think this should be
993 removed. For now, I enable it for DJGPP v2. -- EZ. */
995 /* KLUDGE ALERT! Temporary fix until I figger why
996 stat() is wrong ... think it's buried in GO32's IDT - Jax */
998 bfd_fatal (archive_filename
);
1001 if (!operation_alters_arch
)
1003 fprintf (stderr
, "%s: ", program_name
);
1004 perror (archive_filename
);
1009 /* If the target isn't set, try to figure out the target to use
1010 for the archive from the first object on the list. */
1011 if (target
== NULL
&& file
!= NULL
)
1015 obj
= bfd_openr (file
, target
);
1018 if (bfd_check_format (obj
, bfd_object
)
1019 && bfd_target_supports_archives (obj
))
1020 target
= bfd_get_target (obj
);
1021 (void) bfd_close (obj
);
1025 /* If we die creating a new archive, don't leave it around. */
1026 output_filename
= xstrdup (archive_filename
);
1028 /* Create an empty archive. */
1029 arch
= bfd_openw (archive_filename
, target
);
1031 || ! bfd_set_format (arch
, bfd_archive
)
1032 || ! bfd_close (arch
))
1033 bfd_fatal (archive_filename
);
1034 else if (!silent_create
)
1035 non_fatal (_("creating %s"), archive_filename
);
1038 arch
= bfd_openr (archive_filename
, target
);
1042 bfd_fatal (archive_filename
);
1045 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1047 bfd_nonfatal (archive_filename
);
1048 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1049 list_matching_formats (matching
);
1053 if ((operation
== replace
|| operation
== quick_append
)
1054 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
1056 /* PR 15140: Catch attempts to convert a normal
1057 archive into a thin archive or vice versa. */
1058 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
1060 fatal (_("Cannot convert existing library %s to thin format"),
1061 bfd_get_filename (arch
));
1064 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
1066 fatal (_("Cannot convert existing thin library %s to normal format"),
1067 bfd_get_filename (arch
));
1072 last_one
= &(arch
->archive_next
);
1073 /* Read all the contents right away, regardless. */
1074 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
1076 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
1078 *last_one
= next_one
;
1079 last_one
= &next_one
->archive_next
;
1081 *last_one
= (bfd
*) NULL
;
1082 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1088 print_contents (bfd
*abfd
)
1090 bfd_size_type ncopied
= 0;
1092 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1095 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1096 /* xgettext:c-format */
1097 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1100 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
1102 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
1103 bfd_fatal (bfd_get_filename (abfd
));
1106 while (ncopied
< size
)
1108 bfd_size_type nread
;
1109 bfd_size_type tocopy
= size
- ncopied
;
1111 if (tocopy
> BUFSIZE
)
1114 nread
= bfd_read (cbuf
, tocopy
, abfd
);
1115 if (nread
!= tocopy
)
1116 /* xgettext:c-format */
1117 fatal (_("%s is not a valid archive"),
1118 bfd_get_filename (abfd
->my_archive
));
1120 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1121 return value to bfd_size_type to avoid comparison between signed and
1123 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1124 fatal ("stdout: %s", strerror (errno
));
1131 static FILE * open_output_file (bfd
*) ATTRIBUTE_RETURNS_NONNULL
;
1134 open_output_file (bfd
* abfd
)
1136 char *alloc
= xstrdup (bfd_get_filename (abfd
));
1138 output_filename
= alloc
;
1140 /* PR binutils/17533: Do not allow directory traversal
1141 outside of the current directory tree - unless the
1142 user has explicitly specified an output directory. */
1143 if (! is_valid_archive_path (output_filename
))
1145 char * base
= (char *) lbasename (output_filename
);
1147 non_fatal (_("illegal output pathname for archive member: %s, using '%s' instead"),
1148 output_filename
, base
);
1149 output_filename
= xstrdup (base
);
1151 alloc
= output_filename
;
1156 size_t len
= strlen (output_dir
);
1160 if (IS_DIR_SEPARATOR (output_dir
[len
- 1]))
1161 output_filename
= concat (output_dir
, output_filename
, NULL
);
1163 output_filename
= concat (output_dir
, "/", output_filename
, NULL
);
1169 printf ("x - %s\n", output_filename
);
1171 FILE * ostream
= fopen (output_filename
, FOPEN_WB
);
1172 if (ostream
== NULL
)
1174 perror (output_filename
);
1181 /* Extract a member of the archive into its own file.
1183 We defer opening the new file until after we have read a BUFSIZ chunk of the
1184 old one, since we know we have just read the archive header for the old
1185 one. Since most members are shorter than BUFSIZ, this means we will read
1186 the old header, read the old data, write a new inode for the new file, and
1187 write the new data, and be done. This 'optimization' is what comes from
1188 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1192 extract_file (bfd
*abfd
)
1198 memset (&buf
, 0, sizeof (buf
));
1200 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1201 /* xgettext:c-format */
1202 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1205 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
1206 bfd_fatal (bfd_get_filename (abfd
));
1211 output_file
= open_output_file (abfd
);
1215 bfd_size_type ncopied
= 0;
1216 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1218 while (ncopied
< size
)
1220 bfd_size_type nread
, tocopy
;
1222 tocopy
= size
- ncopied
;
1223 if (tocopy
> BUFSIZE
)
1226 nread
= bfd_read (cbuf
, tocopy
, abfd
);
1227 if (nread
!= tocopy
)
1228 /* xgettext:c-format */
1229 fatal (_("%s is not a valid archive"),
1230 bfd_get_filename (abfd
->my_archive
));
1232 /* See comment above; this saves disk arm motion. */
1233 if (output_file
== NULL
)
1234 output_file
= open_output_file (abfd
);
1236 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1237 the return value to bfd_size_type to avoid comparison between
1238 signed and unsigned values. */
1239 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, output_file
) != nread
)
1240 fatal ("%s: %s", output_filename
, strerror (errno
));
1248 fclose (output_file
);
1252 chmod (output_filename
, buf
.st_mode
);
1256 /* Set access time to modification time. Only st_mtime is
1257 initialized by bfd_stat_arch_elt. */
1258 buf
.st_atime
= buf
.st_mtime
;
1259 set_times (output_filename
, &buf
);
1262 free (output_filename
);
1263 output_filename
= NULL
;
1267 write_archive (bfd
*iarch
)
1270 const char *old_name
;
1272 bfd
*contents_head
= iarch
->archive_next
;
1275 old_name
= bfd_get_filename (iarch
);
1276 new_name
= make_tempname (old_name
, &tmpfd
);
1278 if (new_name
== NULL
)
1279 bfd_fatal (_("could not create temporary file whilst writing archive"));
1281 free (output_filename
);
1282 output_filename
= new_name
;
1284 obfd
= bfd_fdopenw (new_name
, bfd_get_target (iarch
), tmpfd
);
1289 bfd_fatal (old_name
);
1292 bfd_set_format (obfd
, bfd_archive
);
1294 /* Request writing the archive symbol table unless we've
1295 been explicitly requested not to. */
1296 obfd
->has_armap
= write_armap
>= 0;
1300 /* This should really use bfd_set_file_flags, but that rejects
1302 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1306 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1309 obfd
->flags
|= BFD_ARCHIVE_FULL_PATH
;
1311 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1312 bfd_set_thin_archive (obfd
, true);
1314 if (!bfd_set_archive_head (obfd
, contents_head
))
1315 bfd_fatal (old_name
);
1317 tmpfd
= dup (tmpfd
);
1318 if (!bfd_close (obfd
))
1319 bfd_fatal (old_name
);
1321 output_filename
= NULL
;
1322 old_name
= xstrdup (old_name
);
1323 /* We don't care if this fails; we might be creating the archive. */
1326 int ret
= smart_rename (new_name
, old_name
, tmpfd
, NULL
, false);
1327 free ((char *) old_name
);
1333 /* Return a pointer to the pointer to the entry which should be rplacd'd
1334 into when altering. DEFAULT_POS should be how to interpret pos_default,
1335 and should be a pos value. */
1338 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1340 bfd
**after_bfd
= contents
;
1342 const char *realposname
;
1344 if (postype
== pos_default
)
1346 realpos
= default_pos
;
1347 realposname
= default_posname
;
1352 realposname
= posname
;
1355 if (realpos
== pos_end
)
1358 after_bfd
= &((*after_bfd
)->archive_next
);
1362 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1363 if (FILENAME_CMP (bfd_get_filename (*after_bfd
), realposname
) == 0)
1365 if (realpos
== pos_after
)
1366 after_bfd
= &(*after_bfd
)->archive_next
;
1374 delete_members (bfd
*arch
, char **files_to_delete
)
1376 bfd
**current_ptr_ptr
;
1378 bool something_changed
= false;
1381 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1383 /* In a.out systems, the armap is optional. It's also called
1384 __.SYMDEF. So if the user asked to delete it, we should remember
1385 that fact. This isn't quite right for COFF systems (where
1386 __.SYMDEF might be regular member), but it's very unlikely
1387 to be a problem. FIXME */
1389 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1391 arch
->has_armap
= false;
1398 current_ptr_ptr
= &(arch
->archive_next
);
1399 while (*current_ptr_ptr
)
1401 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1402 bfd_get_filename (*current_ptr_ptr
)) == 0)
1405 if (counted_name_mode
1406 && match_count
!= counted_name_counter
)
1408 /* Counting, and didn't match on count; go on to the
1414 something_changed
= true;
1418 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1423 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1426 if (verbose
&& !found
)
1428 /* xgettext:c-format */
1429 printf (_("No member named `%s'\n"), *files_to_delete
);
1435 if (something_changed
)
1436 write_archive (arch
);
1439 free (output_filename
);
1440 output_filename
= NULL
;
1445 /* Reposition existing members within an archive */
1448 move_members (bfd
*arch
, char **files_to_move
)
1450 bfd
**after_bfd
; /* New entries go after this one */
1451 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1453 for (; *files_to_move
; ++files_to_move
)
1455 current_ptr_ptr
= &(arch
->archive_next
);
1456 while (*current_ptr_ptr
)
1458 bfd
*current_ptr
= *current_ptr_ptr
;
1459 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1460 bfd_get_filename (current_ptr
)) == 0)
1462 /* Move this file to the end of the list - first cut from
1465 *current_ptr_ptr
= current_ptr
->archive_next
;
1467 /* Now glue to end */
1468 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1469 link_bfd
= *after_bfd
;
1470 *after_bfd
= current_ptr
;
1471 current_ptr
->archive_next
= link_bfd
;
1474 printf ("m - %s\n", *files_to_move
);
1479 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1481 /* xgettext:c-format */
1482 fatal (_("no entry %s in archive %s!"), *files_to_move
,
1483 bfd_get_filename (arch
));
1488 write_archive (arch
);
1491 /* Ought to default to replacing in place, but this is existing practice! */
1494 replace_members (bfd
*arch
, char **files_to_move
, bool quick
)
1496 bool changed
= false;
1497 bfd
**after_bfd
; /* New entries go after this one. */
1501 while (files_to_move
&& *files_to_move
)
1505 current_ptr
= &arch
->archive_next
;
1506 while (*current_ptr
)
1508 current
= *current_ptr
;
1510 /* For compatibility with existing ar programs, we
1511 permit the same file to be added multiple times. */
1512 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1513 normalize (bfd_get_filename (current
), arch
)) == 0
1514 && current
->arelt_data
!= NULL
)
1520 struct stat fsbuf
, asbuf
;
1522 if (stat (*files_to_move
, &fsbuf
) != 0)
1524 if (errno
!= ENOENT
)
1525 bfd_fatal (*files_to_move
);
1529 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1530 /* xgettext:c-format */
1531 fatal (_("internal stat error on %s"),
1532 bfd_get_filename (current
));
1534 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1535 /* A note about deterministic timestamps: In an
1536 archive created in a determistic manner the
1537 individual elements will either have a timestamp
1538 of 0 or SOURCE_DATE_EPOCH, depending upon the
1539 method used. This will be the value retrieved
1540 by bfd_stat_arch_elt().
1542 The timestamp in fsbuf.st_mtime however will
1543 definitely be greater than 0, and it is unlikely
1544 to be less than SOURCE_DATE_EPOCH. (FIXME:
1545 should we test for this case case and issue an
1548 So in either case fsbuf.st_mtime > asbuf.st_time
1549 and hence the incoming file will replace the
1550 current file. Which is what should be expected to
1551 happen. Deterministic archives have no real sense
1552 of the time/date when their elements were created,
1553 and so any updates to the archive should always
1554 result in replaced files. */
1558 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1559 bfd_get_filename (current
));
1560 if (libdeps_bfd
!= NULL
1561 && FILENAME_CMP (normalize (*files_to_move
, arch
),
1564 replaced
= ar_emul_replace_bfd (after_bfd
, libdeps_bfd
,
1569 replaced
= ar_emul_replace (after_bfd
, *files_to_move
,
1574 /* Snip out this entry from the chain. */
1575 *current_ptr
= (*current_ptr
)->archive_next
;
1581 current_ptr
= &(current
->archive_next
);
1585 /* Add to the end of the archive. */
1586 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1588 if (libdeps_bfd
!= NULL
1589 && FILENAME_CMP (normalize (*files_to_move
, arch
), LIBDEPS
) == 0)
1591 changed
|= ar_emul_append_bfd (after_bfd
, libdeps_bfd
,
1592 verbose
, make_thin_archive
);
1596 changed
|= ar_emul_append (after_bfd
, *files_to_move
, target
,
1597 verbose
, make_thin_archive
);
1606 write_archive (arch
);
1609 free (output_filename
);
1610 output_filename
= NULL
;
1615 ranlib_only (const char *archname
)
1619 if (get_file_size (archname
) < 1)
1622 arch
= open_inarch (archname
, (char *) NULL
);
1625 write_archive (arch
);
1629 /* Update the timestamp of the symbol map of an archive. */
1632 ranlib_touch (const char *archname
)
1635 /* I don't think updating works on go32. */
1636 ranlib_only (archname
);
1642 if (get_file_size (archname
) < 1)
1644 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1647 bfd_set_error (bfd_error_system_call
);
1648 bfd_fatal (archname
);
1651 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1653 bfd_fatal (archname
);
1654 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1656 bfd_nonfatal (archname
);
1657 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1658 list_matching_formats (matching
);
1662 if (! bfd_has_map (arch
))
1663 /* xgettext:c-format */
1664 fatal (_("%s: no archive map to update"), archname
);
1667 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1669 bfd_update_armap_timestamp (arch
);
1671 if (! bfd_close (arch
))
1672 bfd_fatal (archname
);
1677 /* Things which are interesting to map over all or some of the files: */
1680 print_descr (bfd
*abfd
)
1682 print_arelt_descr (stdout
, abfd
, verbose
, display_offsets
);