ar.c 42 KB

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