1 /* bucomm.c -- Bin Utils COMmon code.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
3 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014
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 3 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. */
28 #include "libiberty.h"
29 #include "filenames.h"
32 #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
)
56 errmsg
= bfd_errmsg (bfd_get_error ());
59 fprintf (stderr
, "%s: %s: %s\n", program_name
, string
, errmsg
);
61 fprintf (stderr
, "%s: %s\n", program_name
, errmsg
);
64 /* Issue a non fatal error message. FILENAME, or if NULL then BFD,
65 are used to indicate the problematic file. SECTION, if non NULL,
66 is used to provide a section name. If FORMAT is non-null, then it
67 is used to print additional information via vfprintf. Finally the
68 bfd error message is printed. In summary, error messages are of
69 one of the following forms:
71 PROGRAM:file: bfd-error-message
72 PROGRAM:file[section]: bfd-error-message
73 PROGRAM:file: printf-message: bfd-error-message
74 PROGRAM:file[section]: printf-message: bfd-error-message. */
77 bfd_nonfatal_message (const char *filename
,
79 const asection
*section
,
80 const char *format
, ...)
83 const char *section_name
;
86 errmsg
= bfd_errmsg (bfd_get_error ());
89 va_start (args
, format
);
90 fprintf (stderr
, "%s", program_name
);
95 filename
= bfd_get_archive_filename (abfd
);
97 section_name
= bfd_get_section_name (abfd
, section
);
100 fprintf (stderr
, ":%s[%s]", filename
, section_name
);
102 fprintf (stderr
, ":%s", filename
);
106 fprintf (stderr
, ": ");
107 vfprintf (stderr
, format
, args
);
109 fprintf (stderr
, ": %s\n", errmsg
);
114 bfd_fatal (const char *string
)
116 bfd_nonfatal (string
);
121 report (const char * format
, va_list args
)
124 fprintf (stderr
, "%s: ", program_name
);
125 vfprintf (stderr
, format
, args
);
130 fatal (const char *format
, ...)
134 va_start (args
, format
);
136 report (format
, args
);
142 non_fatal (const char *format
, ...)
146 va_start (args
, format
);
148 report (format
, args
);
152 /* Set the default BFD target based on the configured target. Doing
153 this permits the binutils to be configured for a particular target,
154 and linked against a shared BFD library which was configured for a
158 set_default_bfd_target (void)
160 /* The macro TARGET is defined by Makefile. */
161 const char *target
= TARGET
;
163 if (! bfd_set_default_target (target
))
164 fatal (_("can't set BFD default target to `%s': %s"),
165 target
, bfd_errmsg (bfd_get_error ()));
168 /* After a FALSE return from bfd_check_format_matches with
169 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
170 the possible matching targets. */
173 list_matching_formats (char **p
)
176 fprintf (stderr
, _("%s: Matching formats:"), program_name
);
178 fprintf (stderr
, " %s", *p
++);
179 fputc ('\n', stderr
);
182 /* List the supported targets. */
185 list_supported_targets (const char *name
, FILE *f
)
188 const char **targ_names
;
191 fprintf (f
, _("Supported targets:"));
193 fprintf (f
, _("%s: supported targets:"), name
);
195 targ_names
= bfd_target_list ();
196 for (t
= 0; targ_names
[t
] != NULL
; t
++)
197 fprintf (f
, " %s", targ_names
[t
]);
202 /* List the supported architectures. */
205 list_supported_architectures (const char *name
, FILE *f
)
208 const char ** arches
;
211 fprintf (f
, _("Supported architectures:"));
213 fprintf (f
, _("%s: supported architectures:"), name
);
215 for (arch
= arches
= bfd_arch_list (); *arch
; arch
++)
216 fprintf (f
, " %s", *arch
);
221 /* The length of the longest architecture name + 1. */
222 #define LONGEST_ARCH sizeof ("powerpc:common")
225 endian_string (enum bfd_endian endian
)
229 case BFD_ENDIAN_BIG
: return _("big endian");
230 case BFD_ENDIAN_LITTLE
: return _("little endian");
231 default: return _("endianness unknown");
235 /* List the targets that BFD is configured to support, each followed
236 by its endianness and the architectures it supports. */
239 display_target_list (void)
245 dummy_name
= make_temp_file (NULL
);
246 for (t
= 0; bfd_target_vector
[t
]; t
++)
248 const bfd_target
*p
= bfd_target_vector
[t
];
249 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
252 printf (_("%s\n (header %s, data %s)\n"), p
->name
,
253 endian_string (p
->header_byteorder
),
254 endian_string (p
->byteorder
));
258 bfd_nonfatal (dummy_name
);
263 if (! bfd_set_format (abfd
, bfd_object
))
265 if (bfd_get_error () != bfd_error_invalid_operation
)
267 bfd_nonfatal (p
->name
);
270 bfd_close_all_done (abfd
);
274 for (a
= bfd_arch_obscure
+ 1; a
< bfd_arch_last
; a
++)
275 if (bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
277 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
278 bfd_close_all_done (abfd
);
286 /* Print a table showing which architectures are supported for entries
287 FIRST through LAST-1 of bfd_target_vector (targets across,
288 architectures down). */
291 display_info_table (int first
, int last
)
298 /* Print heading of target names. */
299 printf ("\n%*s", (int) LONGEST_ARCH
, " ");
300 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
301 printf ("%s ", bfd_target_vector
[t
]->name
);
304 dummy_name
= make_temp_file (NULL
);
305 for (a
= bfd_arch_obscure
+ 1; a
< bfd_arch_last
; a
++)
306 if (strcmp (bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0),
309 printf ("%*s ", (int) LONGEST_ARCH
- 1,
310 bfd_printable_arch_mach ((enum bfd_architecture
) a
, 0));
311 for (t
= first
; t
< last
&& bfd_target_vector
[t
]; t
++)
313 const bfd_target
*p
= bfd_target_vector
[t
];
314 bfd_boolean ok
= TRUE
;
315 bfd
*abfd
= bfd_openw (dummy_name
, p
->name
);
319 bfd_nonfatal (p
->name
);
326 if (! bfd_set_format (abfd
, bfd_object
))
328 if (bfd_get_error () != bfd_error_invalid_operation
)
330 bfd_nonfatal (p
->name
);
339 if (! bfd_set_arch_mach (abfd
, (enum bfd_architecture
) a
, 0))
344 printf ("%s ", p
->name
);
347 int l
= strlen (p
->name
);
353 bfd_close_all_done (abfd
);
363 /* Print tables of all the target-architecture combinations that
364 BFD has been configured to support. */
367 display_target_tables (void)
375 colum
= getenv ("COLUMNS");
377 columns
= atoi (colum
);
382 while (bfd_target_vector
[t
] != NULL
)
386 wid
= LONGEST_ARCH
+ strlen (bfd_target_vector
[t
]->name
) + 1;
388 while (wid
< columns
&& bfd_target_vector
[t
] != NULL
)
392 newwid
= wid
+ strlen (bfd_target_vector
[t
]->name
) + 1;
393 if (newwid
>= columns
)
398 if (! display_info_table (oldt
, t
))
408 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING
);
409 if (! display_target_list () || ! display_target_tables ())
415 /* Display the archive header for an element as if it were an ls -l listing:
417 Mode User\tGroup\tSize\tDate Name */
420 print_arelt_descr (FILE *file
, bfd
*abfd
, bfd_boolean verbose
)
426 if (bfd_stat_arch_elt (abfd
, &buf
) == 0)
430 time_t when
= buf
.st_mtime
;
431 const char *ctime_result
= (const char *) ctime (&when
);
434 /* POSIX format: skip weekday and seconds from ctime output. */
435 sprintf (timebuf
, "%.12s %.4s", ctime_result
+ 4, ctime_result
+ 20);
437 mode_string (buf
.st_mode
, modebuf
);
440 /* POSIX 1003.2/D11 says to skip first character (entry type). */
441 fprintf (file
, "%s %ld/%ld %6" BFD_VMA_FMT
"u %s ", modebuf
+ 1,
442 (long) buf
.st_uid
, (long) buf
.st_gid
,
447 fprintf (file
, "%s\n", bfd_get_filename (abfd
));
450 /* Return a path for a new temporary file in the same directory
454 template_in_dir (const char *path
)
456 #define template "stXXXXXX"
457 const char *slash
= strrchr (path
, '/');
461 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
463 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
464 char *bslash
= strrchr (path
, '\\');
466 if (slash
== NULL
|| (bslash
!= NULL
&& bslash
> slash
))
468 if (slash
== NULL
&& path
[0] != '\0' && path
[1] == ':')
473 if (slash
!= (char *) NULL
)
476 tmpname
= (char *) xmalloc (len
+ sizeof (template) + 2);
477 memcpy (tmpname
, path
, len
);
479 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
480 /* If tmpname is "X:", appending a slash will make it a root
481 directory on drive X, which is NOT the same as the current
482 directory on drive X. */
483 if (len
== 2 && tmpname
[1] == ':')
484 tmpname
[len
++] = '.';
486 tmpname
[len
++] = '/';
490 tmpname
= (char *) xmalloc (sizeof (template));
494 memcpy (tmpname
+ len
, template, sizeof (template));
499 /* Return the name of a created temporary file in the same directory
503 make_tempname (char *filename
)
505 char *tmpname
= template_in_dir (filename
);
509 fd
= mkstemp (tmpname
);
511 tmpname
= mktemp (tmpname
);
514 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_EXCL
, 0600);
525 /* Return the name of a created temporary directory inside the
526 directory containing FILENAME. */
529 make_tempdir (char *filename
)
531 char *tmpname
= template_in_dir (filename
);
534 return mkdtemp (tmpname
);
536 tmpname
= mktemp (tmpname
);
539 #if defined (_WIN32) && !defined (__CYGWIN32__)
540 if (mkdir (tmpname
) != 0)
543 if (mkdir (tmpname
, 0700) != 0)
550 /* Parse a string into a VMA, with a fatal error if it can't be
554 parse_vma (const char *s
, const char *arg
)
559 ret
= bfd_scan_vma (s
, &end
, 0);
562 fatal (_("%s: bad number: %s"), arg
, s
);
567 /* Returns the size of the named file. If the file does not
568 exist, or if it is not a real file, then a suitable non-fatal
569 error message is printed and (off_t) -1 is returned. */
572 get_file_size (const char * file_name
)
576 if (stat (file_name
, &statbuf
) < 0)
579 non_fatal (_("'%s': No such file"), file_name
);
581 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
582 file_name
, strerror (errno
));
584 else if (! S_ISREG (statbuf
.st_mode
))
585 non_fatal (_("Warning: '%s' is not an ordinary file"), file_name
);
586 else if (statbuf
.st_size
< 0)
587 non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
590 return statbuf
.st_size
;
595 /* Return the filename in a static buffer. */
598 bfd_get_archive_filename (const bfd
*abfd
)
600 static size_t curr
= 0;
604 assert (abfd
!= NULL
);
606 if (!abfd
->my_archive
)
607 return bfd_get_filename (abfd
);
609 needed
= (strlen (bfd_get_filename (abfd
->my_archive
))
610 + strlen (bfd_get_filename (abfd
)) + 3);
615 curr
= needed
+ (needed
>> 1);
616 buf
= (char *) bfd_malloc (curr
);
617 /* If we can't malloc, fail safe by returning just the file name.
618 This function is only used when building error messages. */
622 return bfd_get_filename (abfd
);
625 sprintf (buf
, "%s(%s)", bfd_get_filename (abfd
->my_archive
),
626 bfd_get_filename (abfd
));