1 /* bucomm.c -- Bin Utils COMmon code.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
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., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
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"
26 #include "bfdver.h"
27 #include "libiberty.h"
28 #include "bucomm.h"
29 #include "filenames.h"
30 #include "libbfd.h"
31
32 #include <sys/stat.h>
33 #include <time.h> /* ctime, maybe time_t */
34 #include <assert.h>
35
36 __RCSID("$MirOS: src/gnu/usr.bin/binutils/binutils/bucomm.c,v 1.4 2005/07/07 16:22:44 tg Exp $");
37
38 #ifndef HAVE_TIME_T_IN_TIME_H
39 #ifndef HAVE_TIME_T_IN_TYPES_H
40 typedef long time_t;
41 #endif
42 #endif
43
44 static const char * endian_string (enum bfd_endian);
45 static int display_target_list (void);
46 static int display_info_table (int, int);
47 static int display_target_tables (void);
48
49 /* Error reporting. */
50
51 char *program_name;
52
53 void
bfd_nonfatal(const char * string)54 bfd_nonfatal (const char *string)
55 {
56 const char *errmsg = bfd_errmsg (bfd_get_error ());
57
58 if (string)
59 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
60 else
61 fprintf (stderr, "%s: %s\n", program_name, errmsg);
62 }
63
64 void
bfd_fatal(const char * string)65 bfd_fatal (const char *string)
66 {
67 bfd_nonfatal (string);
68 xexit (1);
69 }
70
71 void
report(const char * format,va_list args)72 report (const char * format, va_list args)
73 {
74 fprintf (stderr, "%s: ", program_name);
75 vfprintf (stderr, format, args);
76 putc ('\n', stderr);
77 }
78
79 void
fatal(const char * format,...)80 fatal VPARAMS ((const char *format, ...))
81 {
82 VA_OPEN (args, format);
83 VA_FIXEDARG (args, const char *, format);
84
85 report (format, args);
86 VA_CLOSE (args);
87 xexit (1);
88 }
89
90 void
non_fatal(const char * format,...)91 non_fatal VPARAMS ((const char *format, ...))
92 {
93 VA_OPEN (args, format);
94 VA_FIXEDARG (args, const char *, format);
95
96 report (format, args);
97 VA_CLOSE (args);
98 }
99
100 /* Set the default BFD target based on the configured target. Doing
101 this permits the binutils to be configured for a particular target,
102 and linked against a shared BFD library which was configured for a
103 different target. */
104
105 void
set_default_bfd_target(void)106 set_default_bfd_target (void)
107 {
108 /* The macro TARGET is defined by Makefile. */
109 const char *target = TARGET;
110
111 if (! bfd_set_default_target (target))
112 fatal (_("can't set BFD default target to `%s': %s"),
113 target, bfd_errmsg (bfd_get_error ()));
114 }
115
116 /* After a FALSE return from bfd_check_format_matches with
117 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
118 the possible matching targets. */
119
120 void
list_matching_formats(char ** p)121 list_matching_formats (char **p)
122 {
123 fprintf (stderr, _("%s: Matching formats:"), program_name);
124 while (*p)
125 fprintf (stderr, " %s", *p++);
126 fputc ('\n', stderr);
127 }
128
129 /* List the supported targets. */
130
131 void
list_supported_targets(const char * name,FILE * f)132 list_supported_targets (const char *name, FILE *f)
133 {
134 int t;
135 const char **targ_names = bfd_target_list ();
136
137 if (name == NULL)
138 fprintf (f, _("Supported targets:"));
139 else
140 fprintf (f, _("%s: supported targets:"), name);
141
142 for (t = 0; targ_names[t] != NULL; t++)
143 fprintf (f, " %s", targ_names[t]);
144 fprintf (f, "\n");
145 free (targ_names);
146 }
147
148 /* List the supported architectures. */
149
150 void
list_supported_architectures(const char * name,FILE * f)151 list_supported_architectures (const char *name, FILE *f)
152 {
153 const char **arch;
154
155 if (name == NULL)
156 fprintf (f, _("Supported architectures:"));
157 else
158 fprintf (f, _("%s: supported architectures:"), name);
159
160 for (arch = bfd_arch_list (); *arch; arch++)
161 fprintf (f, " %s", *arch);
162 fprintf (f, "\n");
163 }
164
165 /* The length of the longest architecture name + 1. */
166 #define LONGEST_ARCH sizeof ("powerpc:common")
167
168 static const char *
endian_string(enum bfd_endian endian)169 endian_string (enum bfd_endian endian)
170 {
171 switch (endian)
172 {
173 case BFD_ENDIAN_BIG: return "big endian";
174 case BFD_ENDIAN_LITTLE: return "little endian";
175 default: return "endianness unknown";
176 }
177 }
178
179 /* List the targets that BFD is configured to support, each followed
180 by its endianness and the architectures it supports. */
181
182 static int
display_target_list(void)183 display_target_list (void)
184 {
185 char *dummy_name;
186 int t;
187 int ret = 1;
188
189 dummy_name = make_temp_file (NULL);
190 for (t = 0; bfd_target_vector[t]; t++)
191 {
192 const bfd_target *p = bfd_target_vector[t];
193 bfd *abfd = bfd_openw (dummy_name, p->name);
194 int a;
195
196 printf ("%s\n (header %s, data %s)\n", p->name,
197 endian_string (p->header_byteorder),
198 endian_string (p->byteorder));
199
200 if (abfd == NULL)
201 {
202 bfd_nonfatal (dummy_name);
203 ret = 0;
204 continue;
205 }
206
207 if (! bfd_set_format (abfd, bfd_object))
208 {
209 if (bfd_get_error () != bfd_error_invalid_operation)
210 {
211 bfd_nonfatal (p->name);
212 ret = 0;
213 }
214 bfd_close_all_done (abfd);
215 continue;
216 }
217
218 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
219 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
220 printf (" %s\n",
221 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
222 bfd_close_all_done (abfd);
223 }
224 unlink (dummy_name);
225 free (dummy_name);
226
227 return ret;
228 }
229
230 /* Print a table showing which architectures are supported for entries
231 FIRST through LAST-1 of bfd_target_vector (targets across,
232 architectures down). */
233
234 static int
display_info_table(int first,int last)235 display_info_table (int first, int last)
236 {
237 int t;
238 int a;
239 int ret = 1;
240 char *dummy_name;
241
242 /* Print heading of target names. */
243 printf ("\n%*s", (int) LONGEST_ARCH, " ");
244 for (t = first; t < last && bfd_target_vector[t]; t++)
245 printf ("%s ", bfd_target_vector[t]->name);
246 putchar ('\n');
247
248 dummy_name = make_temp_file (NULL);
249 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
250 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
251 {
252 printf ("%*s ", (int) LONGEST_ARCH - 1,
253 bfd_printable_arch_mach (a, 0));
254 for (t = first; t < last && bfd_target_vector[t]; t++)
255 {
256 const bfd_target *p = bfd_target_vector[t];
257 bfd_boolean ok = TRUE;
258 bfd *abfd = bfd_openw (dummy_name, p->name);
259
260 if (abfd == NULL)
261 {
262 bfd_nonfatal (p->name);
263 ret = 0;
264 ok = FALSE;
265 }
266
267 if (ok)
268 {
269 if (! bfd_set_format (abfd, bfd_object))
270 {
271 if (bfd_get_error () != bfd_error_invalid_operation)
272 {
273 bfd_nonfatal (p->name);
274 ret = 0;
275 }
276 ok = FALSE;
277 }
278 }
279
280 if (ok)
281 {
282 if (! bfd_set_arch_mach (abfd, a, 0))
283 ok = FALSE;
284 }
285
286 if (ok)
287 printf ("%s ", p->name);
288 else
289 {
290 int l = strlen (p->name);
291 while (l--)
292 putchar ('-');
293 putchar (' ');
294 }
295 if (abfd != NULL)
296 bfd_close_all_done (abfd);
297 }
298 putchar ('\n');
299 }
300 unlink (dummy_name);
301 free (dummy_name);
302
303 return ret;
304 }
305
306 /* Print tables of all the target-architecture combinations that
307 BFD has been configured to support. */
308
309 static int
display_target_tables(void)310 display_target_tables (void)
311 {
312 int t;
313 int columns;
314 int ret = 1;
315 char *colum;
316
317 columns = 0;
318 colum = getenv ("COLUMNS");
319 if (colum != NULL)
320 columns = atoi (colum);
321 if (columns == 0)
322 columns = 80;
323
324 t = 0;
325 while (bfd_target_vector[t] != NULL)
326 {
327 int oldt = t, wid;
328
329 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
330 ++t;
331 while (wid < columns && bfd_target_vector[t] != NULL)
332 {
333 int newwid;
334
335 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
336 if (newwid >= columns)
337 break;
338 wid = newwid;
339 ++t;
340 }
341 if (! display_info_table (oldt, t))
342 ret = 0;
343 }
344
345 return ret;
346 }
347
348 int
display_info(void)349 display_info (void)
350 {
351 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
352 if (! display_target_list () || ! display_target_tables ())
353 return 1;
354 else
355 return 0;
356 }
357
358 /* Display the archive header for an element as if it were an ls -l listing:
359
360 Mode User\tGroup\tSize\tDate Name */
361
362 void
print_arelt_descr(FILE * file,bfd * abfd,bfd_boolean verbose)363 print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
364 {
365 struct stat buf;
366
367 if (verbose)
368 {
369 if (bfd_stat_arch_elt (abfd, &buf) == 0)
370 {
371 char modebuf[11];
372 char timebuf[40];
373 time_t when = buf.st_mtime;
374 const char *ctime_result = (const char *) ctime (&when);
375
376 /* POSIX format: skip weekday and seconds from ctime output. */
377 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
378
379 mode_string (buf.st_mode, modebuf);
380 modebuf[10] = '\0';
381 /* POSIX 1003.2/D11 says to skip first character (entry type). */
382 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
383 (long) buf.st_uid, (long) buf.st_gid,
384 (long) buf.st_size, timebuf);
385 }
386 }
387
388 fprintf (file, "%s\n", bfd_get_filename (abfd));
389 }
390
391 /* Return the name of a temporary file in the same directory as FILENAME. */
392
393 char *
make_tempname(char * filename,int isdir)394 make_tempname (char *filename, int isdir)
395 {
396 static char template[] = "stXXXXXX";
397 char *tmpname;
398 char *slash = strrchr (filename, '/');
399 char c = 0;
400
401 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
402 {
403 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
404 char *bslash = strrchr (filename, '\\');
405 if (slash == NULL || (bslash != NULL && bslash > slash))
406 slash = bslash;
407 if (slash == NULL && filename[0] != '\0' && filename[1] == ':')
408 slash = filename + 1;
409 }
410 #endif
411
412 if (slash != (char *) NULL)
413 {
414 c = *slash;
415 *slash = 0;
416 tmpname = xmalloc (strlen (filename) + sizeof (template) + 2);
417 strcpy (tmpname, filename);
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 (tmpname[1] == ':' && tmpname[2] == '\0')
423 strcat (tmpname, ".");
424 #endif
425 strcat (tmpname, "/");
426 strcat (tmpname, template);
427 }
428 else
429 {
430 tmpname = xmalloc (sizeof (template));
431 strcpy (tmpname, template);
432 }
433
434 if (isdir)
435 {
436 #ifdef HAVE_MKDTEMP
437 if (mkdtemp (tmpname) == NULL)
438 #else
439 mktemp (tmpname);
440 #if defined (_WIN32) && !defined (__CYGWIN32__)
441 if (mkdir (tmpname))
442 #else
443 if (mkdir (tmpname, 0700))
444 #endif
445 #endif
446 tmpname = NULL;
447 }
448 else
449 {
450 #ifdef HAVE_MKSTEMP
451 int fd;
452
453 if ((fd = mkstemp (tmpname)) == -1)
454 tmpname = NULL;
455 else
456 close (fd);
457 #else
458 mktemp (tmpname);
459 #endif
460 }
461 if (slash != NULL)
462 *slash = c;
463 return tmpname;
464 }
465
466 /* Parse a string into a VMA, with a fatal error if it can't be
467 parsed. */
468
469 bfd_vma
parse_vma(const char * s,const char * arg)470 parse_vma (const char *s, const char *arg)
471 {
472 bfd_vma ret;
473 const char *end;
474
475 ret = bfd_scan_vma (s, &end, 0);
476
477 if (*end != '\0')
478 fatal (_("%s: bad number: %s"), arg, s);
479
480 return ret;
481 }
482
483 /* Returns the size of the named file. If the file does not
484 exist, or if it is not a real file, then a suitable non-fatal
485 error message is printed and zero is returned. */
486
487 off_t
get_file_size(const char * file_name)488 get_file_size (const char * file_name)
489 {
490 struct stat statbuf;
491
492 if (stat (file_name, &statbuf) < 0)
493 {
494 if (errno == ENOENT)
495 non_fatal (_("'%s': No such file"), file_name);
496 else
497 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
498 file_name, strerror (errno));
499 }
500 else if (! S_ISREG (statbuf.st_mode))
501 non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
502 else
503 return statbuf.st_size;
504
505 return 0;
506 }
507
508 /* Return the filename in a static buffer. */
509
510 const char *
bfd_get_archive_filename(bfd * abfd)511 bfd_get_archive_filename (bfd *abfd)
512 {
513 static size_t curr = 0;
514 static char *buf;
515 size_t needed;
516
517 assert (abfd != NULL);
518
519 if (!abfd->my_archive)
520 return bfd_get_filename (abfd);
521
522 needed = (strlen (bfd_get_filename (abfd->my_archive))
523 + strlen (bfd_get_filename (abfd)) + 3);
524 if (needed > curr)
525 {
526 if (curr)
527 free (buf);
528 curr = needed + (needed >> 1);
529 buf = bfd_malloc (curr);
530 /* If we can't malloc, fail safe by returning just the file name.
531 This function is only used when building error messages. */
532 if (!buf)
533 {
534 curr = 0;
535 return bfd_get_filename (abfd);
536 }
537 }
538 sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
539 bfd_get_filename (abfd));
540 return buf;
541 }
542