]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/bucomm.c
Fix typo in previous patch.
[thirdparty/binutils-gdb.git] / binutils / bucomm.c
CommitLineData
252b5132 1/* bucomm.c -- Bin Utils COMmon code.
06d86cf7 2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003
37cc8ec1 3 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21\f
22/* We might put this in a library someday so it could be dynamically
23 loaded, but for now it's not necessary. */
24
25#include "bfd.h"
06d86cf7 26#include "bfdver.h"
252b5132
RH
27#include "libiberty.h"
28#include "bucomm.h"
5af11cab 29#include "filenames.h"
06d86cf7 30#include "libbfd.h"
252b5132
RH
31
32#include <sys/stat.h>
33#include <time.h> /* ctime, maybe time_t */
34
35#ifndef HAVE_TIME_T_IN_TIME_H
36#ifndef HAVE_TIME_T_IN_TYPES_H
37typedef long time_t;
38#endif
39#endif
06d86cf7 40
2da42df6
AJ
41static const char * endian_string (enum bfd_endian);
42static int display_target_list (void);
43static int display_info_table (int, int);
44static int display_target_tables (void);
252b5132 45\f
06d86cf7 46/* Error reporting. */
252b5132
RH
47
48char *program_name;
49
50void
2da42df6 51bfd_nonfatal (const char *string)
252b5132 52{
b1f88ebe 53 const char *errmsg = bfd_errmsg (bfd_get_error ());
252b5132
RH
54
55 if (string)
56 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
57 else
58 fprintf (stderr, "%s: %s\n", program_name, errmsg);
59}
60
61void
2da42df6 62bfd_fatal (const char *string)
252b5132
RH
63{
64 bfd_nonfatal (string);
65 xexit (1);
66}
67
37cc8ec1 68void
2da42df6 69report (const char * format, va_list args)
252b5132
RH
70{
71 fprintf (stderr, "%s: ", program_name);
72 vfprintf (stderr, format, args);
73 putc ('\n', stderr);
74}
75
252b5132 76void
451dad9c 77fatal VPARAMS ((const char *format, ...))
252b5132 78{
451dad9c
AM
79 VA_OPEN (args, format);
80 VA_FIXEDARG (args, const char *, format);
252b5132 81
252b5132 82 report (format, args);
451dad9c 83 VA_CLOSE (args);
252b5132
RH
84 xexit (1);
85}
86
87void
451dad9c 88non_fatal VPARAMS ((const char *format, ...))
252b5132 89{
451dad9c
AM
90 VA_OPEN (args, format);
91 VA_FIXEDARG (args, const char *, format);
252b5132 92
252b5132 93 report (format, args);
451dad9c 94 VA_CLOSE (args);
252b5132 95}
252b5132
RH
96
97/* Set the default BFD target based on the configured target. Doing
98 this permits the binutils to be configured for a particular target,
99 and linked against a shared BFD library which was configured for a
100 different target. */
101
102void
2da42df6 103set_default_bfd_target (void)
252b5132
RH
104{
105 /* The macro TARGET is defined by Makefile. */
106 const char *target = TARGET;
107
108 if (! bfd_set_default_target (target))
109 fatal (_("can't set BFD default target to `%s': %s"),
110 target, bfd_errmsg (bfd_get_error ()));
111}
112
b34976b6 113/* After a FALSE return from bfd_check_format_matches with
252b5132
RH
114 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
115 the possible matching targets. */
116
117void
2da42df6 118list_matching_formats (char **p)
252b5132
RH
119{
120 fprintf (stderr, _("%s: Matching formats:"), program_name);
121 while (*p)
122 fprintf (stderr, " %s", *p++);
123 fputc ('\n', stderr);
124}
125
126/* List the supported targets. */
127
128void
2da42df6 129list_supported_targets (const char *name, FILE *f)
252b5132 130{
252b5132 131 int t;
48417c1a 132 const char **targ_names = bfd_target_list ();
252b5132
RH
133
134 if (name == NULL)
135 fprintf (f, _("Supported targets:"));
136 else
137 fprintf (f, _("%s: supported targets:"), name);
48417c1a
AM
138
139 for (t = 0; targ_names[t] != NULL; t++)
140 fprintf (f, " %s", targ_names[t]);
252b5132 141 fprintf (f, "\n");
48417c1a 142 free (targ_names);
252b5132 143}
2f83960e
AM
144
145/* List the supported architectures. */
146
147void
2da42df6 148list_supported_architectures (const char *name, FILE *f)
2f83960e 149{
48417c1a 150 const char **arch;
2f83960e
AM
151
152 if (name == NULL)
153 fprintf (f, _("Supported architectures:"));
154 else
155 fprintf (f, _("%s: supported architectures:"), name);
156
157 for (arch = bfd_arch_list (); *arch; arch++)
158 fprintf (f, " %s", *arch);
159 fprintf (f, "\n");
160}
252b5132 161\f
06d86cf7
NC
162/* The length of the longest architecture name + 1. */
163#define LONGEST_ARCH sizeof ("powerpc:common")
164
165static const char *
2da42df6 166endian_string (enum bfd_endian endian)
06d86cf7
NC
167{
168 switch (endian)
169 {
170 case BFD_ENDIAN_BIG: return "big endian";
171 case BFD_ENDIAN_LITTLE: return "little endian";
172 default: return "endianness unknown";
173 }
174}
175
176/* List the targets that BFD is configured to support, each followed
177 by its endianness and the architectures it supports. */
178
179static int
2da42df6 180display_target_list (void)
06d86cf7
NC
181{
182 char *dummy_name;
183 int t;
184 int ret = 1;
185
186 dummy_name = make_temp_file (NULL);
187 for (t = 0; bfd_target_vector[t]; t++)
188 {
189 const bfd_target *p = bfd_target_vector[t];
190 bfd *abfd = bfd_openw (dummy_name, p->name);
191 int a;
192
193 printf ("%s\n (header %s, data %s)\n", p->name,
194 endian_string (p->header_byteorder),
195 endian_string (p->byteorder));
196
197 if (abfd == NULL)
198 {
199 bfd_nonfatal (dummy_name);
200 ret = 0;
201 continue;
202 }
203
204 if (! bfd_set_format (abfd, bfd_object))
205 {
206 if (bfd_get_error () != bfd_error_invalid_operation)
207 {
208 bfd_nonfatal (p->name);
209 ret = 0;
210 }
211 bfd_close_all_done (abfd);
212 continue;
213 }
214
215 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
216 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
217 printf (" %s\n",
218 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
219 bfd_close_all_done (abfd);
220 }
221 unlink (dummy_name);
222 free (dummy_name);
223
224 return ret;
225}
226
227/* Print a table showing which architectures are supported for entries
228 FIRST through LAST-1 of bfd_target_vector (targets across,
229 architectures down). */
230
231static int
2da42df6 232display_info_table (int first, int last)
06d86cf7
NC
233{
234 int t;
235 int a;
236 int ret = 1;
237 char *dummy_name;
238
239 /* Print heading of target names. */
240 printf ("\n%*s", (int) LONGEST_ARCH, " ");
241 for (t = first; t < last && bfd_target_vector[t]; t++)
242 printf ("%s ", bfd_target_vector[t]->name);
243 putchar ('\n');
244
245 dummy_name = make_temp_file (NULL);
246 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
247 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
248 {
249 printf ("%*s ", (int) LONGEST_ARCH - 1,
250 bfd_printable_arch_mach (a, 0));
251 for (t = first; t < last && bfd_target_vector[t]; t++)
252 {
253 const bfd_target *p = bfd_target_vector[t];
254 bfd_boolean ok = TRUE;
255 bfd *abfd = bfd_openw (dummy_name, p->name);
256
257 if (abfd == NULL)
258 {
259 bfd_nonfatal (p->name);
260 ret = 0;
261 ok = FALSE;
262 }
263
264 if (ok)
265 {
266 if (! bfd_set_format (abfd, bfd_object))
267 {
268 if (bfd_get_error () != bfd_error_invalid_operation)
269 {
270 bfd_nonfatal (p->name);
271 ret = 0;
272 }
273 ok = FALSE;
274 }
275 }
276
277 if (ok)
278 {
279 if (! bfd_set_arch_mach (abfd, a, 0))
280 ok = FALSE;
281 }
282
283 if (ok)
284 printf ("%s ", p->name);
285 else
286 {
287 int l = strlen (p->name);
288 while (l--)
289 putchar ('-');
290 putchar (' ');
291 }
292 if (abfd != NULL)
293 bfd_close_all_done (abfd);
294 }
295 putchar ('\n');
296 }
297 unlink (dummy_name);
298 free (dummy_name);
299
300 return ret;
301}
302
303/* Print tables of all the target-architecture combinations that
304 BFD has been configured to support. */
305
306static int
2da42df6 307display_target_tables (void)
06d86cf7
NC
308{
309 int t;
310 int columns;
311 int ret = 1;
312 char *colum;
313
314 columns = 0;
315 colum = getenv ("COLUMNS");
316 if (colum != NULL)
317 columns = atoi (colum);
318 if (columns == 0)
319 columns = 80;
320
321 t = 0;
322 while (bfd_target_vector[t] != NULL)
323 {
324 int oldt = t, wid;
325
326 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
327 ++t;
328 while (wid < columns && bfd_target_vector[t] != NULL)
329 {
330 int newwid;
331
332 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
333 if (newwid >= columns)
334 break;
335 wid = newwid;
336 ++t;
337 }
338 if (! display_info_table (oldt, t))
339 ret = 0;
340 }
341
342 return ret;
343}
344
345int
2da42df6 346display_info (void)
06d86cf7
NC
347{
348 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
349 if (! display_target_list () || ! display_target_tables ())
350 return 1;
351 else
352 return 0;
353}
354\f
252b5132
RH
355/* Display the archive header for an element as if it were an ls -l listing:
356
357 Mode User\tGroup\tSize\tDate Name */
358
359void
2da42df6 360print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
252b5132
RH
361{
362 struct stat buf;
363
364 if (verbose)
365 {
366 if (bfd_stat_arch_elt (abfd, &buf) == 0)
367 {
368 char modebuf[11];
369 char timebuf[40];
370 time_t when = buf.st_mtime;
b1f88ebe 371 const char *ctime_result = (const char *) ctime (&when);
252b5132
RH
372
373 /* POSIX format: skip weekday and seconds from ctime output. */
374 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
375
376 mode_string (buf.st_mode, modebuf);
377 modebuf[10] = '\0';
378 /* POSIX 1003.2/D11 says to skip first character (entry type). */
379 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
380 (long) buf.st_uid, (long) buf.st_gid,
381 (long) buf.st_size, timebuf);
382 }
383 }
384
385 fprintf (file, "%s\n", bfd_get_filename (abfd));
386}
387
388/* Return the name of a temporary file in the same directory as FILENAME. */
389
390char *
2da42df6 391make_tempname (char *filename)
252b5132
RH
392{
393 static char template[] = "stXXXXXX";
394 char *tmpname;
395 char *slash = strrchr (filename, '/');
396
5af11cab
AM
397#ifdef HAVE_DOS_BASED_FILE_SYSTEM
398 {
399 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
400 char *bslash = strrchr (filename, '\\');
2ab47eed 401 if (slash == NULL || (bslash != NULL && bslash > slash))
5af11cab
AM
402 slash = bslash;
403 if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
a0c0ddf7 404 slash = filename + 1;
5af11cab 405 }
252b5132
RH
406#endif
407
408 if (slash != (char *) NULL)
409 {
410 char c;
411
412 c = *slash;
413 *slash = 0;
5af11cab 414 tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
252b5132 415 strcpy (tmpname, filename);
5af11cab
AM
416#ifdef HAVE_DOS_BASED_FILE_SYSTEM
417 /* If tmpname is "X:", appending a slash will make it a root
418 directory on drive X, which is NOT the same as the current
419 directory on drive X. */
420 if (tmpname[1] == ':' && tmpname[2] == '\0')
421 strcat (tmpname, ".");
422#endif
252b5132
RH
423 strcat (tmpname, "/");
424 strcat (tmpname, template);
af667503 425 mktemp (tmpname);
252b5132
RH
426 *slash = c;
427 }
428 else
429 {
430 tmpname = xmalloc (sizeof (template));
431 strcpy (tmpname, template);
af667503 432 mktemp (tmpname);
252b5132
RH
433 }
434 return tmpname;
435}
436
437/* Parse a string into a VMA, with a fatal error if it can't be
438 parsed. */
439
440bfd_vma
2da42df6 441parse_vma (const char *s, const char *arg)
252b5132
RH
442{
443 bfd_vma ret;
444 const char *end;
445
446 ret = bfd_scan_vma (s, &end, 0);
f462a9ea 447
252b5132
RH
448 if (*end != '\0')
449 fatal (_("%s: bad number: %s"), arg, s);
450
451 return ret;
452}