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