gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / binutils / arsup.c
CommitLineData
252b5132 1/* arsup.c - Archive support for MRI compatibility
b3adc24a 2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
252b5132 3
3a1a2036 4 This file is part of GNU Binutils.
252b5132 5
3a1a2036
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
3a1a2036 9 (at your option) any later version.
252b5132 10
3a1a2036
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
3a1a2036
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
21
22/* Contributed by Steve Chamberlain
3a1a2036 23 sac@cygnus.com
252b5132 24
3a1a2036
NC
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST. */
252b5132 27
3db64b00 28#include "sysdep.h"
252b5132 29#include "bfd.h"
252b5132 30#include "libiberty.h"
5af11cab 31#include "filenames.h"
3db64b00
AM
32#include "bucomm.h"
33#include "arsup.h"
252b5132
RH
34
35static void map_over_list
2da42df6
AJ
36 (bfd *, void (*function) (bfd *, bfd *), struct list *);
37static void ar_directory_doer (bfd *, bfd *);
38static void ar_addlib_doer (bfd *, bfd *);
252b5132
RH
39
40extern int verbose;
5cd84a72 41extern int deterministic;
252b5132 42
85b1c36d
BE
43static bfd *obfd;
44static char *real_name;
45static FILE *outfile;
46
252b5132 47static void
2da42df6 48map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
252b5132
RH
49{
50 bfd *head;
51
52 if (list == NULL)
53 {
54 bfd *next;
55
cc481421 56 head = arch->archive_next;
252b5132
RH
57 while (head != NULL)
58 {
cc481421 59 next = head->archive_next;
252b5132
RH
60 function (head, (bfd *) NULL);
61 head = next;
62 }
63 }
64 else
65 {
66 struct list *ptr;
67
68 /* This may appear to be a baroque way of accomplishing what we
69 want. however we have to iterate over the filenames in order
70 to notice where a filename is requested but does not exist in
71 the archive. Ditto mapping over each file each time -- we
72 want to hack multiple references. */
73 for (ptr = list; ptr; ptr = ptr->next)
74 {
b34976b6 75 bfd_boolean found = FALSE;
252b5132
RH
76 bfd *prev = arch;
77
cc481421 78 for (head = arch->archive_next; head; head = head->archive_next)
252b5132 79 {
c177f377
AM
80 if (bfd_get_filename (head) != NULL
81 && FILENAME_CMP (ptr->name, bfd_get_filename (head)) == 0)
252b5132 82 {
b34976b6 83 found = TRUE;
252b5132
RH
84 function (head, prev);
85 }
86 prev = head;
87 }
88 if (! found)
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
90 }
91 }
92}
93
94
252b5132 95
252b5132 96static void
2da42df6 97ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
252b5132 98{
1869e86f 99 print_arelt_descr(outfile, abfd, verbose, FALSE);
252b5132
RH
100}
101
102void
2da42df6 103ar_directory (char *ar_name, struct list *list, char *output)
252b5132
RH
104{
105 bfd *arch;
106
107 arch = open_inarch (ar_name, (char *) NULL);
108 if (output)
109 {
110 outfile = fopen(output,"w");
111 if (outfile == 0)
112 {
113 outfile = stdout;
114 fprintf (stderr,_("Can't open file %s\n"), output);
115 output = 0;
116 }
117 }
f462a9ea 118 else
252b5132
RH
119 outfile = stdout;
120
121 map_over_list (arch, ar_directory_doer, list);
122
123 bfd_close (arch);
124
125 if (output)
126 fclose (outfile);
127}
128
129void
2da42df6 130prompt (void)
252b5132
RH
131{
132 extern int interactive;
3a1a2036 133
f462a9ea 134 if (interactive)
3a1a2036
NC
135 {
136 printf ("AR >");
137 fflush (stdout);
138 }
252b5132
RH
139}
140
141void
2da42df6 142maybequit (void)
252b5132 143{
f462a9ea 144 if (! interactive)
252b5132
RH
145 xexit (9);
146}
147
148
3a1a2036 149void
2da42df6 150ar_open (char *name, int t)
252b5132 151{
2e02f296 152 char *tname;
5e9520c8 153 const char *bname = lbasename (name);
252b5132 154 real_name = name;
3a1a2036 155
5af11cab
AM
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */
2e02f296
CZ
158 if (asprintf (&tname, "%.*stmp-%s", (int) (bname - name), name, bname) == -1)
159 {
160 fprintf (stderr, _("%s: Can't allocate memory for temp name (%s)\n"),
161 program_name, strerror(errno));
162 maybequit ();
163 return;
164 }
165
3a1a2036
NC
166 obfd = bfd_openw (tname, NULL);
167
168 if (!obfd)
169 {
170 fprintf (stderr,
171 _("%s: Can't open output archive %s\n"),
172 program_name, tname);
173
174 maybequit ();
252b5132 175 }
3a1a2036
NC
176 else
177 {
178 if (!t)
179 {
180 bfd **ptr;
181 bfd *element;
182 bfd *ibfd;
252b5132 183
3a1a2036 184 ibfd = bfd_openr (name, NULL);
252b5132 185
3a1a2036
NC
186 if (!ibfd)
187 {
188 fprintf (stderr,_("%s: Can't open input archive %s\n"),
189 program_name, name);
190 maybequit ();
191 return;
192 }
193
b34976b6 194 if (!bfd_check_format(ibfd, bfd_archive))
3a1a2036
NC
195 {
196 fprintf (stderr,
197 _("%s: file %s is not an archive\n"),
198 program_name, name);
199 maybequit ();
200 return;
201 }
202
203 ptr = &(obfd->archive_head);
204 element = bfd_openr_next_archived_file (ibfd, NULL);
205
206 while (element)
207 {
208 *ptr = element;
cc481421 209 ptr = &element->archive_next;
3a1a2036
NC
210 element = bfd_openr_next_archived_file (ibfd, element);
211 }
212 }
213
214 bfd_set_format (obfd, bfd_archive);
252b5132 215
3a1a2036 216 obfd->has_armap = 1;
a8da6403 217 obfd->is_thin_archive = 0;
3a1a2036
NC
218 }
219}
252b5132
RH
220
221static void
2da42df6 222ar_addlib_doer (bfd *abfd, bfd *prev)
252b5132 223{
3a1a2036 224 /* Add this module to the output bfd. */
252b5132 225 if (prev != NULL)
cc481421 226 prev->archive_next = abfd->archive_next;
3a1a2036 227
cc481421 228 abfd->archive_next = obfd->archive_head;
252b5132
RH
229 obfd->archive_head = abfd;
230}
231
232void
2da42df6 233ar_addlib (char *name, struct list *list)
252b5132
RH
234{
235 if (obfd == NULL)
236 {
237 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
238 maybequit ();
239 }
240 else
241 {
242 bfd *arch;
243
244 arch = open_inarch (name, (char *) NULL);
245 if (arch != NULL)
246 map_over_list (arch, ar_addlib_doer, list);
247
50c2245b 248 /* Don't close the bfd, since it will make the elements disappear. */
252b5132
RH
249 }
250}
251
252void
2da42df6 253ar_addmod (struct list *list)
252b5132 254{
3a1a2036
NC
255 if (!obfd)
256 {
257 fprintf (stderr, _("%s: no open output archive\n"), program_name);
258 maybequit ();
259 }
f462a9ea 260 else
3a1a2036
NC
261 {
262 while (list)
263 {
70b0cf90 264 bfd *abfd;
3a1a2036 265
70b0cf90
NC
266#if BFD_SUPPORTS_PLUGINS
267 abfd = bfd_openr (list->name, "plugin");
268#else
269 abfd = bfd_openr (list->name, NULL);
270#endif
3a1a2036
NC
271 if (!abfd)
272 {
273 fprintf (stderr, _("%s: can't open file %s\n"),
274 program_name, list->name);
275 maybequit ();
276 }
277 else
278 {
cc481421 279 abfd->archive_next = obfd->archive_head;
3a1a2036
NC
280 obfd->archive_head = abfd;
281 }
282 list = list->next;
283 }
252b5132 284 }
252b5132
RH
285}
286
287
252b5132 288void
2da42df6 289ar_clear (void)
252b5132 290{
3a1a2036
NC
291 if (obfd)
292 obfd->archive_head = 0;
252b5132
RH
293}
294
295void
2da42df6 296ar_delete (struct list *list)
252b5132 297{
3a1a2036
NC
298 if (!obfd)
299 {
300 fprintf (stderr, _("%s: no open output archive\n"), program_name);
301 maybequit ();
302 }
f462a9ea 303 else
3a1a2036
NC
304 {
305 while (list)
306 {
307 /* Find this name in the archive. */
308 bfd *member = obfd->archive_head;
309 bfd **prev = &(obfd->archive_head);
310 int found = 0;
311
312 while (member)
313 {
c177f377 314 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
3a1a2036 315 {
cc481421 316 *prev = member->archive_next;
3a1a2036
NC
317 found = 1;
318 }
319 else
cc481421 320 prev = &(member->archive_next);
3a1a2036 321
cc481421 322 member = member->archive_next;
3a1a2036
NC
323 }
324
325 if (!found)
326 {
327 fprintf (stderr, _("%s: can't find module file %s\n"),
328 program_name, list->name);
329 maybequit ();
330 }
331
332 list = list->next;
252b5132 333 }
252b5132 334 }
252b5132
RH
335}
336
252b5132 337void
2da42df6 338ar_save (void)
252b5132 339{
3a1a2036
NC
340 if (!obfd)
341 {
342 fprintf (stderr, _("%s: no open output archive\n"), program_name);
343 maybequit ();
344 }
345 else
346 {
347 char *ofilename = xstrdup (bfd_get_filename (obfd));
252b5132 348
5cd84a72
NC
349 if (deterministic > 0)
350 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
351
3a1a2036 352 bfd_close (obfd);
252b5132 353
1ba93119 354 smart_rename (ofilename, real_name, 0);
3a1a2036
NC
355 obfd = 0;
356 free (ofilename);
357 }
358}
252b5132
RH
359
360void
2da42df6 361ar_replace (struct list *list)
252b5132 362{
3a1a2036
NC
363 if (!obfd)
364 {
365 fprintf (stderr, _("%s: no open output archive\n"), program_name);
366 maybequit ();
367 }
f462a9ea 368 else
3a1a2036
NC
369 {
370 while (list)
252b5132 371 {
3a1a2036
NC
372 /* Find this name in the archive. */
373 bfd *member = obfd->archive_head;
374 bfd **prev = &(obfd->archive_head);
375 int found = 0;
376
377 while (member)
378 {
c177f377 379 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
3a1a2036
NC
380 {
381 /* Found the one to replace. */
70b0cf90 382 bfd *abfd = bfd_openr (list->name, NULL);
3a1a2036
NC
383
384 if (!abfd)
385 {
386 fprintf (stderr, _("%s: can't open file %s\n"),
387 program_name, list->name);
388 maybequit ();
389 }
390 else
391 {
392 *prev = abfd;
cc481421 393 abfd->archive_next = member->archive_next;
3a1a2036
NC
394 found = 1;
395 }
396 }
397 else
398 {
cc481421 399 prev = &(member->archive_next);
3a1a2036 400 }
cc481421 401 member = member->archive_next;
3a1a2036
NC
402 }
403
404 if (!found)
405 {
70b0cf90 406 bfd *abfd = bfd_openr (list->name, NULL);
252b5132 407
3a1a2036
NC
408 fprintf (stderr,_("%s: can't find module file %s\n"),
409 program_name, list->name);
410 if (!abfd)
411 {
412 fprintf (stderr, _("%s: can't open file %s\n"),
413 program_name, list->name);
414 maybequit ();
415 }
416 else
417 *prev = abfd;
418 }
419
420 list = list->next;
421 }
252b5132 422 }
252b5132
RH
423}
424
3a1a2036 425/* And I added this one. */
252b5132 426void
2da42df6 427ar_list (void)
252b5132 428{
f462a9ea 429 if (!obfd)
252b5132 430 {
3a1a2036
NC
431 fprintf (stderr, _("%s: no open output archive\n"), program_name);
432 maybequit ();
252b5132 433 }
3a1a2036
NC
434 else
435 {
436 bfd *abfd;
437
438 outfile = stdout;
439 verbose =1 ;
440 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
252b5132 441
3a1a2036
NC
442 for (abfd = obfd->archive_head;
443 abfd != (bfd *)NULL;
cc481421 444 abfd = abfd->archive_next)
3a1a2036
NC
445 ar_directory_doer (abfd, (bfd *) NULL);
446 }
447}
252b5132 448
f462a9ea 449void
2da42df6 450ar_end (void)
252b5132
RH
451{
452 if (obfd)
3a1a2036 453 {
c92c35e7 454 bfd_cache_close (obfd);
3a1a2036
NC
455 unlink (bfd_get_filename (obfd));
456 }
252b5132 457}
3a1a2036 458
252b5132 459void
2da42df6 460ar_extract (struct list *list)
252b5132 461{
f462a9ea 462 if (!obfd)
3a1a2036
NC
463 {
464 fprintf (stderr, _("%s: no open archive\n"), program_name);
465 maybequit ();
466 }
f462a9ea 467 else
3a1a2036
NC
468 {
469 while (list)
252b5132 470 {
3a1a2036
NC
471 /* Find this name in the archive. */
472 bfd *member = obfd->archive_head;
473 int found = 0;
474
475 while (member && !found)
476 {
c177f377 477 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
3a1a2036
NC
478 {
479 extract_file (member);
480 found = 1;
481 }
482
cc481421 483 member = member->archive_next;
3a1a2036
NC
484 }
485
486 if (!found)
487 {
70b0cf90 488 bfd_openr (list->name, NULL);
3a1a2036
NC
489 fprintf (stderr, _("%s: can't find module file %s\n"),
490 program_name, list->name);
491 }
492
493 list = list->next;
494 }
252b5132 495 }
252b5132 496}
This page took 0.793215 seconds and 4 git commands to generate.