1 /* bucomm.c -- Bin Utils COMmon code.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* We might put this in a library someday so it could be dynamically
24 loaded, but for now it's not necessary. */
27 #include "libiberty.h"
29 #include "filenames.h"
33 #include <time.h> /* ctime, maybe time_t */
36 #ifndef HAVE_TIME_T_IN_TIME_H
37 #ifndef HAVE_TIME_T_IN_TYPES_H
42 static const char * endian_string (enum bfd_endian
);
43 static int display_target_list (void);
44 static int display_info_table (int, int);
45 static int display_target_tables (void);
47 /* Error reporting. */
52 bfd_nonfatal (const char *string
)
54 const char *errmsg
= bfd_errmsg (bfd_get_error ());
57 fprintf (stderr
, "%s: %s: %s\n", program_name
, string
, errmsg
);
59 fprintf (stderr
, "%s: %s\n", program_name
, errmsg
);
63 bfd_fatal (const char *string
)
65 bfd_nonfatal (string
);
70 report (const char * format
, va_list args
)
72 fprintf (stderr
, "%s: ", program_name
);
73 vfprintf (stderr
, format
, args
);
78 fatal
VPARAMS ((const char *format
, ...))
80 VA_OPEN (args
, format
);
81 VA_FIXEDARG (args
, const char *, format
);
83 report (format
, args
);
89 non_fatal
VPARAMS ((const char *format
, ...))
91 VA_OPEN (args
, format
);
92 VA_FIXEDARG (args
, const char *, format
);
94 report (format
, args
);
98 /* Set the default BFD target based on the configured target. Doing
99 this permits the binutils to be configured for a particular target,
100 and linked against a shared BFD library which was configured for a
104 set_default_bfd_target (void)
106 /* The macro TARGET is defined by Makefile. */
107 const char *target
= TARGET
;
109 if (! bfd_set_default_target (target
))
110 fatal (_("can't set BFD default target to `%s': %s"),
111 target
, bfd_errmsg (bfd_get_error ()));
114 /* After a FALSE return from bfd_check_format_matches with
115 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
116 the possible matching targets. */
119 list_matching_formats (char **p
)
121 fprintf (stderr
, _("%s: Matching formats:"), program_name
);
123 fprintf (stderr
, " %s", *p
++);
124 fputc ('\n', stderr
);
127 /* List the supported targets. */
130 list_supported_targets (const char *name
, FILE *f
)
133 const char **targ_names
= bfd_target_list ();
136 fprintf (f
, _("Supported targets:"));
138 fprintf (f
, _("%s: supported targets:"), name
);
140 for (t
= 0; targ_names
[t
] != NULL
; t
++)
141 fprintf (f
, " %s", targ_names
[t
]);
146 /* List the supported architectures. */
149 list_supported_architectures (const char *name
, FILE *f
)
154 fprintf (f
, _("Supported architectures:"));
156 fprintf (f
, _("%s: supported architectures:"), name
);
158 for (arch
= bfd_arch_list (); *arch
; arch
++)
159 fprintf (f
, " %s", *arch
);
163 /* The length of the longest architecture name + 1. */
164 #define LONGEST_ARCH sizeof ("powerpc:common")
167 endian_string (enum bfd_endian endian
)
171 case BFD_ENDIAN_BIG
: return "big endian";
172 case BFD_ENDIAN_LITTLE
: return "little endian";
173 default: return "endianness unknown";
177 /* List the targets that BFD is configured to support, each followed
178 by its endianness and the architectures it supports. */
181 display_target_list (void)
187 dummy_name
= make_temp_file (NULL
);
188 for (t
= 0; bfd_target_vector
[t
]; t
++)
190 const bfd_target
*p
= bfd_target_vector
[t
];
191 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
192 enum bfd_architecture a
;
194 printf ("%s\n (header %s, data %s)\n", p
->name
,
195 endian_string (p
->header_byteorder
),
196 endian_string (p
->byteorder
));
200 bfd_nonfatal (dummy_name
);
205 if (! bfd_set_format (abfd
, bfd_object
))
207 if (bfd_get_error () != bfd_error_invalid_operation
)
209 bfd_nonfatal (p
->name
);
212 bfd_close_all_done (abfd
);
216 for (a
= bfd_arch_obscure
+ 1; a
< bfd_arch_last
; a
++)
217 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
219 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
220 bfd_close_all_done (abfd
);
228 /* Print a table showing which architectures are supported for entries
229 FIRST through LAST-1 of bfd_target_vector (targets across,
230 architectures down). */
233 display_info_table (int first
, int last
)
238 enum bfd_architecture a
;
240 /* Print heading of target names. */
241 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
242 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
243 printf ("%s ", bfd_target_vector
[t
]->name
);
246 dummy_name
= make_temp_file (NULL
);
247 for (a
= bfd_arch_obscure
+ 1; a
< bfd_arch_last
; a
++)
248 if (strcmp (bfd_printable_arch_mach (a
, 0), "UNKNOWN!") != 0)
250 printf ("%*s ", (int) LONGEST_ARCH
- 1,
251 bfd_printable_arch_mach (a
, 0));
252 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
254 const bfd_target
*p
= bfd_target_vector
[t
];
255 bfd_boolean ok
= TRUE
;
256 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
260 bfd_nonfatal (p
->name
);
267 if (! bfd_set_format (abfd
, bfd_object
))
269 if (bfd_get_error () != bfd_error_invalid_operation
)
271 bfd_nonfatal (p
->name
);
280 if (! bfd_set_arch_mach (abfd
, a
, 0))
285 printf ("%s ", p
->name
);
288 int l
= strlen (p
->name
);
294 bfd_close_all_done (abfd
);
304 /* Print tables of all the target-architecture combinations that
305 BFD has been configured to support. */
308 display_target_tables (void)
316 colum
= getenv ("COLUMNS");
318 columns
= atoi (colum
);
323 while (bfd_target_vector
[t
] != NULL
)
327 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
329 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
333 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
334 if (newwid
>= columns
)
339 if (! display_info_table (oldt
, t
))
349 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING
);
350 if (! display_target_list () || ! display_target_tables ())
356 /* Display the archive header for an element as if it were an ls -l listing:
358 Mode User\tGroup\tSize\tDate Name */
361 print_arelt_descr (FILE *file
, bfd
*abfd
, bfd_boolean verbose
)
367 if (bfd_stat_arch_elt (abfd
, &buf
) == 0)
371 time_t when
= buf
.st_mtime
;
372 const char *ctime_result
= (const char *) ctime (&when
);
374 /* POSIX format: skip weekday and seconds from ctime output. */
375 sprintf (timebuf
, "%.12s %.4s", ctime_result
+ 4, ctime_result
+ 20);
377 mode_string (buf
.st_mode
, modebuf
);
379 /* POSIX 1003.2/D11 says to skip first character (entry type). */
380 fprintf (file
, "%s %ld/%ld %6ld %s ", modebuf
+ 1,
381 (long) buf
.st_uid
, (long) buf
.st_gid
,
382 (long) buf
.st_size
, timebuf
);
386 fprintf (file
, "%s\n", bfd_get_filename (abfd
));
389 /* Return a path for a new temporary file in the same directory
393 template_in_dir (const char *path
)
395 #define template "stXXXXXX"
396 const char *slash
= strrchr (path
, '/');
400 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
402 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
403 char *bslash
= strrchr (path
, '\\');
405 if (slash
== NULL
|| (bslash
!= NULL
&& bslash
> slash
))
407 if (slash
== NULL
&& path
[0] != '\0' && path
[1] == ':')
412 if (slash
!= (char *) NULL
)
415 tmpname
= xmalloc (len
+ sizeof (template) + 2);
416 memcpy (tmpname
, path
, len
);
418 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
419 /* If tmpname is "X:", appending a slash will make it a root
420 directory on drive X, which is NOT the same as the current
421 directory on drive X. */
422 if (len
== 2 && tmpname
[1] == ':')
423 tmpname
[len
++] = '.';
425 tmpname
[len
++] = '/';
429 tmpname
= xmalloc (sizeof (template));
433 memcpy (tmpname
+ len
, template, sizeof (template));
438 /* Return the name of a created temporary file in the same directory
442 make_tempname (char *filename
)
444 char *tmpname
= template_in_dir (filename
);
448 fd
= mkstemp (tmpname
);
450 tmpname
= mktemp (tmpname
);
453 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
461 /* Return the name of a created temporary directory inside the
462 directory containing FILENAME. */
465 make_tempdir (char *filename
)
467 char *tmpname
= template_in_dir (filename
);
470 return mkdtemp (tmpname
);
472 tmpname
= mktemp (tmpname
);
475 #if defined (_WIN32) && !defined (__CYGWIN32__)
476 if (mkdir (tmpname
) != 0)
479 if (mkdir (tmpname
, 0700) != 0)
486 /* Parse a string into a VMA, with a fatal error if it can't be
490 parse_vma (const char *s
, const char *arg
)
495 ret
= bfd_scan_vma (s
, &end
, 0);
498 fatal (_("%s: bad number: %s"), arg
, s
);
503 /* Returns the size of the named file. If the file does not
504 exist, or if it is not a real file, then a suitable non-fatal
505 error message is printed and zero is returned. */
508 get_file_size (const char * file_name
)
512 if (stat (file_name
, &statbuf
) < 0)
515 non_fatal (_("'%s': No such file"), file_name
);
517 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
518 file_name
, strerror (errno
));
520 else if (! S_ISREG (statbuf
.st_mode
))
521 non_fatal (_("Warning: '%s' is not an ordinary file"), file_name
);
523 return statbuf
.st_size
;
528 /* Return the filename in a static buffer. */
531 bfd_get_archive_filename (bfd
*abfd
)
533 static size_t curr
= 0;
537 assert (abfd
!= NULL
);
539 if (!abfd
->my_archive
)
540 return bfd_get_filename (abfd
);
542 needed
= (strlen (bfd_get_filename (abfd
->my_archive
))
543 + strlen (bfd_get_filename (abfd
)) + 3);
548 curr
= needed
+ (needed
>> 1);
549 buf
= bfd_malloc (curr
);
550 /* If we can't malloc, fail safe by returning just the file name.
551 This function is only used when building error messages. */
555 return bfd_get_filename (abfd
);
558 sprintf (buf
, "%s(%s)", bfd_get_filename (abfd
->my_archive
),
559 bfd_get_filename (abfd
));