source: trunk/src/binutils/binutils/bucomm.c@ 618

Last change on this file since 618 was 618, checked in by bird, 22 years ago

Joined the port of 2.11.2 with 2.14.

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