* objdump.c (dump_reloc_set): Free malloced memory.
[deliverable/binutils-gdb.git] / binutils / bucomm.c
CommitLineData
252b5132 1/* bucomm.c -- Bin Utils COMmon code.
485be063 2 Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
9849fbfc 3 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
37cc8ec1 4 Free Software Foundation, Inc.
252b5132
RH
5
6 This file is part of GNU Binutils.
7
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
32866df7 10 the Free Software Foundation; either version 3 of the License, or
252b5132
RH
11 (at your option) any later version.
12
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.
17
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
b43b5d5f
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
252b5132
RH
22\f
23/* We might put this in a library someday so it could be dynamically
24 loaded, but for now it's not necessary. */
25
3db64b00 26#include "sysdep.h"
252b5132
RH
27#include "bfd.h"
28#include "libiberty.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 */
77f762d6 34#include <assert.h>
3db64b00 35#include "bucomm.h"
252b5132
RH
36
37#ifndef HAVE_TIME_T_IN_TIME_H
38#ifndef HAVE_TIME_T_IN_TYPES_H
39typedef long time_t;
40#endif
41#endif
06d86cf7 42
2da42df6
AJ
43static const char * endian_string (enum bfd_endian);
44static int display_target_list (void);
45static int display_info_table (int, int);
46static int display_target_tables (void);
252b5132 47\f
06d86cf7 48/* Error reporting. */
252b5132
RH
49
50char *program_name;
51
52void
2da42df6 53bfd_nonfatal (const char *string)
252b5132 54{
a8c62f1c 55 const char *errmsg;
252b5132 56
a8c62f1c 57 errmsg = bfd_errmsg (bfd_get_error ());
8e085dd2 58 fflush (stdout);
252b5132
RH
59 if (string)
60 fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
61 else
62 fprintf (stderr, "%s: %s\n", program_name, errmsg);
63}
64
2db6cde7
NS
65/* Issue a non fatal error message. FILENAME, or if NULL then BFD,
66 are used to indicate the problematic file. SECTION, if non NULL,
67 is used to provide a section name. If FORMAT is non-null, then it
68 is used to print additional information via vfprintf. Finally the
69 bfd error message is printed. In summary, error messages are of
70 one of the following forms:
71
72 PROGRAM:file: bfd-error-message
73 PROGRAM:file[section]: bfd-error-message
74 PROGRAM:file: printf-message: bfd-error-message
91d6fa6a 75 PROGRAM:file[section]: printf-message: bfd-error-message. */
2db6cde7
NS
76
77void
78bfd_nonfatal_message (const char *filename,
91d6fa6a
NC
79 const bfd *abfd,
80 const asection *section,
2db6cde7
NS
81 const char *format, ...)
82{
a8c62f1c
AM
83 const char *errmsg;
84 const char *section_name;
2db6cde7
NS
85 va_list args;
86
a8c62f1c 87 errmsg = bfd_errmsg (bfd_get_error ());
8e085dd2 88 fflush (stdout);
a8c62f1c 89 section_name = NULL;
2db6cde7
NS
90 va_start (args, format);
91 fprintf (stderr, "%s", program_name);
92
91d6fa6a 93 if (abfd)
2db6cde7
NS
94 {
95 if (!filename)
91d6fa6a 96 filename = bfd_get_archive_filename (abfd);
2db6cde7 97 if (section)
91d6fa6a 98 section_name = bfd_get_section_name (abfd, section);
2db6cde7
NS
99 }
100 if (section_name)
101 fprintf (stderr, ":%s[%s]", filename, section_name);
102 else
103 fprintf (stderr, ":%s", filename);
104
105 if (format)
106 {
107 fprintf (stderr, ": ");
108 vfprintf (stderr, format, args);
109 }
110 fprintf (stderr, ": %s\n", errmsg);
111 va_end (args);
112}
113
252b5132 114void
2da42df6 115bfd_fatal (const char *string)
252b5132
RH
116{
117 bfd_nonfatal (string);
118 xexit (1);
119}
120
cba12006 121void
2da42df6 122report (const char * format, va_list args)
252b5132 123{
a8c62f1c 124 fflush (stdout);
252b5132
RH
125 fprintf (stderr, "%s: ", program_name);
126 vfprintf (stderr, format, args);
127 putc ('\n', stderr);
128}
129
252b5132 130void
451dad9c 131fatal VPARAMS ((const char *format, ...))
252b5132 132{
451dad9c
AM
133 VA_OPEN (args, format);
134 VA_FIXEDARG (args, const char *, format);
252b5132 135
252b5132 136 report (format, args);
451dad9c 137 VA_CLOSE (args);
252b5132
RH
138 xexit (1);
139}
140
141void
451dad9c 142non_fatal VPARAMS ((const char *format, ...))
252b5132 143{
451dad9c
AM
144 VA_OPEN (args, format);
145 VA_FIXEDARG (args, const char *, format);
252b5132 146
252b5132 147 report (format, args);
451dad9c 148 VA_CLOSE (args);
252b5132 149}
252b5132
RH
150
151/* Set the default BFD target based on the configured target. Doing
152 this permits the binutils to be configured for a particular target,
153 and linked against a shared BFD library which was configured for a
154 different target. */
155
156void
2da42df6 157set_default_bfd_target (void)
252b5132
RH
158{
159 /* The macro TARGET is defined by Makefile. */
160 const char *target = TARGET;
161
162 if (! bfd_set_default_target (target))
163 fatal (_("can't set BFD default target to `%s': %s"),
164 target, bfd_errmsg (bfd_get_error ()));
165}
166
b34976b6 167/* After a FALSE return from bfd_check_format_matches with
252b5132
RH
168 bfd_get_error () == bfd_error_file_ambiguously_recognized, print
169 the possible matching targets. */
170
171void
2da42df6 172list_matching_formats (char **p)
252b5132 173{
a8c62f1c 174 fflush (stdout);
252b5132
RH
175 fprintf (stderr, _("%s: Matching formats:"), program_name);
176 while (*p)
177 fprintf (stderr, " %s", *p++);
178 fputc ('\n', stderr);
179}
180
181/* List the supported targets. */
182
183void
2da42df6 184list_supported_targets (const char *name, FILE *f)
252b5132 185{
252b5132 186 int t;
a8c62f1c 187 const char **targ_names;
252b5132
RH
188
189 if (name == NULL)
190 fprintf (f, _("Supported targets:"));
191 else
192 fprintf (f, _("%s: supported targets:"), name);
48417c1a 193
a8c62f1c 194 targ_names = bfd_target_list ();
48417c1a
AM
195 for (t = 0; targ_names[t] != NULL; t++)
196 fprintf (f, " %s", targ_names[t]);
252b5132 197 fprintf (f, "\n");
48417c1a 198 free (targ_names);
252b5132 199}
2f83960e
AM
200
201/* List the supported architectures. */
202
203void
2da42df6 204list_supported_architectures (const char *name, FILE *f)
2f83960e 205{
d25576aa
NC
206 const char ** arch;
207 const char ** arches;
2f83960e
AM
208
209 if (name == NULL)
210 fprintf (f, _("Supported architectures:"));
211 else
212 fprintf (f, _("%s: supported architectures:"), name);
213
d25576aa 214 for (arch = arches = bfd_arch_list (); *arch; arch++)
2f83960e
AM
215 fprintf (f, " %s", *arch);
216 fprintf (f, "\n");
d25576aa 217 free (arches);
2f83960e 218}
252b5132 219\f
06d86cf7
NC
220/* The length of the longest architecture name + 1. */
221#define LONGEST_ARCH sizeof ("powerpc:common")
222
223static const char *
2da42df6 224endian_string (enum bfd_endian endian)
06d86cf7
NC
225{
226 switch (endian)
227 {
228 case BFD_ENDIAN_BIG: return "big endian";
229 case BFD_ENDIAN_LITTLE: return "little endian";
230 default: return "endianness unknown";
231 }
232}
233
234/* List the targets that BFD is configured to support, each followed
235 by its endianness and the architectures it supports. */
236
237static int
2da42df6 238display_target_list (void)
06d86cf7
NC
239{
240 char *dummy_name;
241 int t;
242 int ret = 1;
243
244 dummy_name = make_temp_file (NULL);
245 for (t = 0; bfd_target_vector[t]; t++)
246 {
247 const bfd_target *p = bfd_target_vector[t];
248 bfd *abfd = bfd_openw (dummy_name, p->name);
3f5e193b 249 int a;
06d86cf7
NC
250
251 printf ("%s\n (header %s, data %s)\n", p->name,
252 endian_string (p->header_byteorder),
253 endian_string (p->byteorder));
254
255 if (abfd == NULL)
256 {
257 bfd_nonfatal (dummy_name);
258 ret = 0;
259 continue;
260 }
261
262 if (! bfd_set_format (abfd, bfd_object))
263 {
264 if (bfd_get_error () != bfd_error_invalid_operation)
265 {
266 bfd_nonfatal (p->name);
267 ret = 0;
268 }
269 bfd_close_all_done (abfd);
270 continue;
271 }
272
91610c0c 273 for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
06d86cf7
NC
274 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
275 printf (" %s\n",
276 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
277 bfd_close_all_done (abfd);
278 }
279 unlink (dummy_name);
280 free (dummy_name);
281
282 return ret;
283}
284
285/* Print a table showing which architectures are supported for entries
286 FIRST through LAST-1 of bfd_target_vector (targets across,
287 architectures down). */
288
289static int
2da42df6 290display_info_table (int first, int last)
06d86cf7
NC
291{
292 int t;
06d86cf7
NC
293 int ret = 1;
294 char *dummy_name;
3f5e193b 295 int a;
06d86cf7
NC
296
297 /* Print heading of target names. */
298 printf ("\n%*s", (int) LONGEST_ARCH, " ");
299 for (t = first; t < last && bfd_target_vector[t]; t++)
300 printf ("%s ", bfd_target_vector[t]->name);
301 putchar ('\n');
302
303 dummy_name = make_temp_file (NULL);
91610c0c 304 for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
3f5e193b
NC
305 if (strcmp (bfd_printable_arch_mach ((enum bfd_architecture) a, 0),
306 "UNKNOWN!") != 0)
06d86cf7
NC
307 {
308 printf ("%*s ", (int) LONGEST_ARCH - 1,
3f5e193b 309 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
06d86cf7
NC
310 for (t = first; t < last && bfd_target_vector[t]; t++)
311 {
312 const bfd_target *p = bfd_target_vector[t];
313 bfd_boolean ok = TRUE;
314 bfd *abfd = bfd_openw (dummy_name, p->name);
315
316 if (abfd == NULL)
317 {
318 bfd_nonfatal (p->name);
319 ret = 0;
320 ok = FALSE;
321 }
322
323 if (ok)
324 {
325 if (! bfd_set_format (abfd, bfd_object))
326 {
327 if (bfd_get_error () != bfd_error_invalid_operation)
328 {
329 bfd_nonfatal (p->name);
330 ret = 0;
331 }
332 ok = FALSE;
333 }
334 }
335
336 if (ok)
337 {
3f5e193b 338 if (! bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
06d86cf7
NC
339 ok = FALSE;
340 }
341
342 if (ok)
343 printf ("%s ", p->name);
344 else
345 {
346 int l = strlen (p->name);
347 while (l--)
348 putchar ('-');
349 putchar (' ');
350 }
351 if (abfd != NULL)
352 bfd_close_all_done (abfd);
353 }
354 putchar ('\n');
355 }
356 unlink (dummy_name);
357 free (dummy_name);
358
359 return ret;
360}
361
362/* Print tables of all the target-architecture combinations that
363 BFD has been configured to support. */
364
365static int
2da42df6 366display_target_tables (void)
06d86cf7
NC
367{
368 int t;
369 int columns;
370 int ret = 1;
371 char *colum;
372
373 columns = 0;
374 colum = getenv ("COLUMNS");
375 if (colum != NULL)
376 columns = atoi (colum);
377 if (columns == 0)
378 columns = 80;
379
380 t = 0;
381 while (bfd_target_vector[t] != NULL)
382 {
383 int oldt = t, wid;
384
385 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
386 ++t;
387 while (wid < columns && bfd_target_vector[t] != NULL)
388 {
389 int newwid;
390
391 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
392 if (newwid >= columns)
393 break;
394 wid = newwid;
395 ++t;
396 }
397 if (! display_info_table (oldt, t))
398 ret = 0;
399 }
400
401 return ret;
402}
403
404int
2da42df6 405display_info (void)
06d86cf7
NC
406{
407 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
408 if (! display_target_list () || ! display_target_tables ())
409 return 1;
410 else
411 return 0;
412}
413\f
252b5132
RH
414/* Display the archive header for an element as if it were an ls -l listing:
415
416 Mode User\tGroup\tSize\tDate Name */
417
418void
2da42df6 419print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
252b5132
RH
420{
421 struct stat buf;
422
423 if (verbose)
424 {
425 if (bfd_stat_arch_elt (abfd, &buf) == 0)
426 {
427 char modebuf[11];
428 char timebuf[40];
429 time_t when = buf.st_mtime;
b1f88ebe 430 const char *ctime_result = (const char *) ctime (&when);
252b5132
RH
431
432 /* POSIX format: skip weekday and seconds from ctime output. */
433 sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
434
435 mode_string (buf.st_mode, modebuf);
436 modebuf[10] = '\0';
437 /* POSIX 1003.2/D11 says to skip first character (entry type). */
438 fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
439 (long) buf.st_uid, (long) buf.st_gid,
440 (long) buf.st_size, timebuf);
441 }
442 }
443
444 fprintf (file, "%s\n", bfd_get_filename (abfd));
445}
446
485be063
AM
447/* Return a path for a new temporary file in the same directory
448 as file PATH. */
252b5132 449
485be063
AM
450static char *
451template_in_dir (const char *path)
252b5132 452{
485be063 453#define template "stXXXXXX"
2946671e 454 const char *slash = strrchr (path, '/');
252b5132 455 char *tmpname;
485be063 456 size_t len;
252b5132 457
5af11cab
AM
458#ifdef HAVE_DOS_BASED_FILE_SYSTEM
459 {
460 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
485be063 461 char *bslash = strrchr (path, '\\');
f9c026a8 462
2ab47eed 463 if (slash == NULL || (bslash != NULL && bslash > slash))
5af11cab 464 slash = bslash;
485be063 465 if (slash == NULL && path[0] != '\0' && path[1] == ':')
2946671e 466 slash = path + 1;
5af11cab 467 }
252b5132
RH
468#endif
469
470 if (slash != (char *) NULL)
471 {
485be063 472 len = slash - path;
3f5e193b 473 tmpname = (char *) xmalloc (len + sizeof (template) + 2);
485be063 474 memcpy (tmpname, path, len);
252b5132 475
5af11cab
AM
476#ifdef HAVE_DOS_BASED_FILE_SYSTEM
477 /* If tmpname is "X:", appending a slash will make it a root
478 directory on drive X, which is NOT the same as the current
479 directory on drive X. */
485be063
AM
480 if (len == 2 && tmpname[1] == ':')
481 tmpname[len++] = '.';
5af11cab 482#endif
485be063 483 tmpname[len++] = '/';
252b5132
RH
484 }
485 else
486 {
3f5e193b 487 tmpname = (char *) xmalloc (sizeof (template));
485be063 488 len = 0;
f9c026a8 489 }
485be063
AM
490
491 memcpy (tmpname + len, template, sizeof (template));
492 return tmpname;
493#undef template
494}
495
496/* Return the name of a created temporary file in the same directory
497 as FILENAME. */
498
499char *
500make_tempname (char *filename)
501{
502 char *tmpname = template_in_dir (filename);
503 int fd;
504
505#ifdef HAVE_MKSTEMP
506 fd = mkstemp (tmpname);
507#else
508 tmpname = mktemp (tmpname);
509 if (tmpname == NULL)
f9c026a8 510 return NULL;
485be063 511 fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
f9c026a8 512#endif
485be063
AM
513 if (fd == -1)
514 return NULL;
515 close (fd);
f9c026a8
NC
516 return tmpname;
517}
518
485be063
AM
519/* Return the name of a created temporary directory inside the
520 directory containing FILENAME. */
f9c026a8
NC
521
522char *
523make_tempdir (char *filename)
524{
485be063 525 char *tmpname = template_in_dir (filename);
f9c026a8 526
485be063
AM
527#ifdef HAVE_MKDTEMP
528 return mkdtemp (tmpname);
529#else
530 tmpname = mktemp (tmpname);
531 if (tmpname == NULL)
532 return NULL;
533#if defined (_WIN32) && !defined (__CYGWIN32__)
534 if (mkdir (tmpname) != 0)
535 return NULL;
536#else
537 if (mkdir (tmpname, 0700) != 0)
538 return NULL;
f9c026a8 539#endif
252b5132 540 return tmpname;
485be063 541#endif
252b5132
RH
542}
543
544/* Parse a string into a VMA, with a fatal error if it can't be
545 parsed. */
546
547bfd_vma
2da42df6 548parse_vma (const char *s, const char *arg)
252b5132
RH
549{
550 bfd_vma ret;
551 const char *end;
552
553 ret = bfd_scan_vma (s, &end, 0);
f462a9ea 554
252b5132
RH
555 if (*end != '\0')
556 fatal (_("%s: bad number: %s"), arg, s);
557
558 return ret;
559}
f24ddbdd
NC
560
561/* Returns the size of the named file. If the file does not
562 exist, or if it is not a real file, then a suitable non-fatal
a3029abd 563 error message is printed and (off_t) -1 is returned. */
f24ddbdd
NC
564
565off_t
566get_file_size (const char * file_name)
567{
568 struct stat statbuf;
569
570 if (stat (file_name, &statbuf) < 0)
571 {
572 if (errno == ENOENT)
573 non_fatal (_("'%s': No such file"), file_name);
574 else
575 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
576 file_name, strerror (errno));
577 }
578 else if (! S_ISREG (statbuf.st_mode))
579 non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
9849fbfc
NC
580 else if (statbuf.st_size < 0)
581 non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
582 file_name);
f24ddbdd
NC
583 else
584 return statbuf.st_size;
585
52a476ee 586 return (off_t) -1;
f24ddbdd 587}
77f762d6
L
588
589/* Return the filename in a static buffer. */
590
591const char *
8d8e0703 592bfd_get_archive_filename (const bfd *abfd)
77f762d6
L
593{
594 static size_t curr = 0;
595 static char *buf;
596 size_t needed;
597
598 assert (abfd != NULL);
599
600 if (!abfd->my_archive)
601 return bfd_get_filename (abfd);
602
603 needed = (strlen (bfd_get_filename (abfd->my_archive))
604 + strlen (bfd_get_filename (abfd)) + 3);
605 if (needed > curr)
606 {
607 if (curr)
608 free (buf);
609 curr = needed + (needed >> 1);
3f5e193b 610 buf = (char *) bfd_malloc (curr);
77f762d6
L
611 /* If we can't malloc, fail safe by returning just the file name.
612 This function is only used when building error messages. */
613 if (!buf)
614 {
615 curr = 0;
616 return bfd_get_filename (abfd);
617 }
618 }
619 sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
620 bfd_get_filename (abfd));
621 return buf;
622}
This page took 0.477845 seconds and 4 git commands to generate.