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