Update year range in copyright notice of binutils files
[deliverable/binutils-gdb.git] / ld / ldmisc.c
1 /* ldmisc.c
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support.
4
5 This file is part of the 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 3 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,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libiberty.h"
26 #include "safe-ctype.h"
27 #include "filenames.h"
28 #include "demangle.h"
29 #include <stdarg.h>
30 #include "ld.h"
31 #include "ldmisc.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldmain.h"
37 #include "ldfile.h"
38 #include "elf-bfd.h"
39 #include "coff-bfd.h"
40
41 /*
42 %% literal %
43 %C clever filename:linenumber with function
44 %D like %C, but no function name
45 %E current bfd error or errno
46 %F error is fatal
47 %G like %D, but only function name
48 %H like %C but in addition emit section+offset
49 %P print program name
50 %V hex bfd_vma
51 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
52 %X no object output, fail return
53 %d integer, like printf
54 %ld long, like printf
55 %lu unsigned long, like printf
56 %p native (host) void* pointer, like printf
57 %pA section name from a section
58 %pB filename from a bfd
59 %pI filename from a lang_input_statement_type
60 %pR info about a relent
61 %pS print script file and linenumber from etree_type.
62 %pT symbol name
63 %s arbitrary string, like printf
64 %u integer, like printf
65 %v hex bfd_vma, no leading zeros
66 */
67
68 void
69 vfinfo (FILE *fp, const char *fmt, va_list ap, bfd_boolean is_warning)
70 {
71 bfd_boolean fatal = FALSE;
72 const char *scan;
73 int arg_type;
74 unsigned int arg_count = 0;
75 unsigned int arg_no;
76 union vfinfo_args
77 {
78 int i;
79 long l;
80 void *p;
81 bfd_vma v;
82 struct {
83 bfd *abfd;
84 asection *sec;
85 bfd_vma off;
86 } reladdr;
87 enum
88 {
89 Bad,
90 Int,
91 Long,
92 Ptr,
93 Vma,
94 RelAddr
95 } type;
96 } args[9];
97
98 for (arg_no = 0; arg_no < sizeof (args) / sizeof (args[0]); arg_no++)
99 args[arg_no].type = Bad;
100
101 arg_count = 0;
102 scan = fmt;
103 while (*scan != '\0')
104 {
105 while (*scan != '%' && *scan != '\0')
106 scan++;
107
108 if (*scan == '%')
109 {
110 scan++;
111
112 arg_no = arg_count;
113 if (*scan != '0' && ISDIGIT (*scan) && scan[1] == '$')
114 {
115 arg_no = *scan - '1';
116 scan += 2;
117 }
118
119 arg_type = Bad;
120 switch (*scan++)
121 {
122 case '\0':
123 --scan;
124 break;
125
126 case 'V':
127 case 'v':
128 case 'W':
129 arg_type = Vma;
130 break;
131
132 case 's':
133 arg_type = Ptr;
134 break;
135
136 case 'p':
137 if (*scan == 'A' || *scan == 'B' || *scan == 'I'
138 || *scan == 'R' || *scan == 'S' || *scan == 'T')
139 scan++;
140 arg_type = Ptr;
141 break;
142
143 case 'C':
144 case 'D':
145 case 'G':
146 case 'H':
147 arg_type = RelAddr;
148 break;
149
150 case 'd':
151 case 'u':
152 arg_type = Int;
153 break;
154
155 case 'l':
156 if (*scan == 'd' || *scan == 'u')
157 {
158 ++scan;
159 arg_type = Long;
160 }
161 break;
162
163 default:
164 break;
165 }
166 if (arg_type != Bad)
167 {
168 if (arg_no >= sizeof (args) / sizeof (args[0]))
169 abort ();
170 args[arg_no].type = arg_type;
171 ++arg_count;
172 }
173 }
174 }
175
176 for (arg_no = 0; arg_no < arg_count; arg_no++)
177 {
178 switch (args[arg_no].type)
179 {
180 case Int:
181 args[arg_no].i = va_arg (ap, int);
182 break;
183 case Long:
184 args[arg_no].l = va_arg (ap, long);
185 break;
186 case Ptr:
187 args[arg_no].p = va_arg (ap, void *);
188 break;
189 case Vma:
190 args[arg_no].v = va_arg (ap, bfd_vma);
191 break;
192 case RelAddr:
193 args[arg_no].reladdr.abfd = va_arg (ap, bfd *);
194 args[arg_no].reladdr.sec = va_arg (ap, asection *);
195 args[arg_no].reladdr.off = va_arg (ap, bfd_vma);
196 break;
197 default:
198 abort ();
199 }
200 }
201
202 arg_count = 0;
203 while (*fmt != '\0')
204 {
205 const char *str = fmt;
206 while (*fmt != '%' && *fmt != '\0')
207 fmt++;
208 if (fmt != str)
209 if (fwrite (str, 1, fmt - str, fp))
210 {
211 /* Ignore. */
212 }
213
214 if (*fmt == '%')
215 {
216 fmt++;
217
218 arg_no = arg_count;
219 if (*fmt != '0' && ISDIGIT (*fmt) && fmt[1] == '$')
220 {
221 arg_no = *fmt - '1';
222 fmt += 2;
223 }
224
225 switch (*fmt++)
226 {
227 case '\0':
228 --fmt;
229 /* Fall through. */
230
231 case '%':
232 /* literal % */
233 putc ('%', fp);
234 break;
235
236 case 'X':
237 /* no object output, fail return */
238 config.make_executable = FALSE;
239 break;
240
241 case 'V':
242 /* hex bfd_vma */
243 {
244 bfd_vma value = args[arg_no].v;
245 ++arg_count;
246 fprintf_vma (fp, value);
247 }
248 break;
249
250 case 'v':
251 /* hex bfd_vma, no leading zeros */
252 {
253 char buf[100];
254 char *p = buf;
255 bfd_vma value = args[arg_no].v;
256 ++arg_count;
257 sprintf_vma (p, value);
258 while (*p == '0')
259 p++;
260 if (!*p)
261 p--;
262 fputs (p, fp);
263 }
264 break;
265
266 case 'W':
267 /* hex bfd_vma with 0x with no leading zeroes taking up
268 8 spaces. */
269 {
270 char buf[100];
271 bfd_vma value;
272 char *p;
273 int len;
274
275 value = args[arg_no].v;
276 ++arg_count;
277 sprintf_vma (buf, value);
278 for (p = buf; *p == '0'; ++p)
279 ;
280 if (*p == '\0')
281 --p;
282 len = strlen (p);
283 while (len < 8)
284 {
285 putc (' ', fp);
286 ++len;
287 }
288 fprintf (fp, "0x%s", p);
289 }
290 break;
291
292 case 'F':
293 /* Error is fatal. */
294 fatal = TRUE;
295 break;
296
297 case 'P':
298 /* Print program name. */
299 fprintf (fp, "%s", program_name);
300 break;
301
302 case 'E':
303 /* current bfd error or errno */
304 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
305 break;
306
307 case 'C':
308 case 'D':
309 case 'G':
310 case 'H':
311 /* Clever filename:linenumber with function name if possible.
312 The arguments are a BFD, a section, and an offset. */
313 {
314 static bfd *last_bfd;
315 static char *last_file;
316 static char *last_function;
317 bfd *abfd;
318 asection *section;
319 bfd_vma offset;
320 asymbol **asymbols = NULL;
321 const char *filename;
322 const char *functionname;
323 unsigned int linenumber;
324 bfd_boolean discard_last;
325 bfd_boolean done;
326
327 abfd = args[arg_no].reladdr.abfd;
328 section = args[arg_no].reladdr.sec;
329 offset = args[arg_no].reladdr.off;
330 ++arg_count;
331
332 if (abfd != NULL)
333 {
334 if (!bfd_generic_link_read_symbols (abfd))
335 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
336
337 asymbols = bfd_get_outsymbols (abfd);
338 }
339
340 /* The GNU Coding Standard requires that error messages
341 be of the form:
342
343 source-file-name:lineno: message
344
345 We do not always have a line number available so if
346 we cannot find them we print out the section name and
347 offset instead. */
348 discard_last = TRUE;
349 if (abfd != NULL
350 && bfd_find_nearest_line (abfd, section, asymbols, offset,
351 &filename, &functionname,
352 &linenumber))
353 {
354 if (functionname != NULL
355 && (fmt[-1] == 'C' || fmt[-1] == 'H'))
356 {
357 /* Detect the case where we are printing out a
358 message for the same function as the last
359 call to vinfo ("%C"). In this situation do
360 not print out the ABFD filename or the
361 function name again. Note - we do still
362 print out the source filename, as this will
363 allow programs that parse the linker's output
364 (eg emacs) to correctly locate multiple
365 errors in the same source file. */
366 if (last_bfd == NULL
367 || last_function == NULL
368 || last_bfd != abfd
369 || (last_file == NULL) != (filename == NULL)
370 || (filename != NULL
371 && filename_cmp (last_file, filename) != 0)
372 || strcmp (last_function, functionname) != 0)
373 {
374 lfinfo (fp, _("%pB: in function `%pT':\n"),
375 abfd, functionname);
376
377 last_bfd = abfd;
378 if (last_file != NULL)
379 free (last_file);
380 last_file = NULL;
381 if (filename)
382 last_file = xstrdup (filename);
383 if (last_function != NULL)
384 free (last_function);
385 last_function = xstrdup (functionname);
386 }
387 discard_last = FALSE;
388 }
389 else
390 lfinfo (fp, "%pB:", abfd);
391
392 if (filename != NULL)
393 fprintf (fp, "%s:", filename);
394
395 done = fmt[-1] != 'H';
396 if (functionname != NULL && fmt[-1] == 'G')
397 lfinfo (fp, "%pT", functionname);
398 else if (filename != NULL && linenumber != 0)
399 fprintf (fp, "%u%s", linenumber, done ? "" : ":");
400 else
401 done = FALSE;
402 }
403 else
404 {
405 lfinfo (fp, "%pB:", abfd);
406 done = FALSE;
407 }
408 if (!done)
409 lfinfo (fp, "(%pA+0x%v)", section, offset);
410
411 if (discard_last)
412 {
413 last_bfd = NULL;
414 if (last_file != NULL)
415 {
416 free (last_file);
417 last_file = NULL;
418 }
419 if (last_function != NULL)
420 {
421 free (last_function);
422 last_function = NULL;
423 }
424 }
425 }
426 break;
427
428 case 'p':
429 if (*fmt == 'A')
430 {
431 /* section name from a section */
432 asection *sec;
433 bfd *abfd;
434 const char *group = NULL;
435 struct coff_comdat_info *ci;
436
437 fmt++;
438 sec = (asection *) args[arg_no].p;
439 ++arg_count;
440 abfd = sec->owner;
441 fprintf (fp, "%s", sec->name);
442 if (abfd != NULL
443 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
444 && elf_next_in_group (sec) != NULL
445 && (sec->flags & SEC_GROUP) == 0)
446 group = elf_group_name (sec);
447 else if (abfd != NULL
448 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
449 && (ci = bfd_coff_get_comdat_section (sec->owner,
450 sec)) != NULL)
451 group = ci->name;
452 if (group != NULL)
453 fprintf (fp, "[%s]", group);
454 }
455 else if (*fmt == 'B')
456 {
457 /* filename from a bfd */
458 bfd *abfd = (bfd *) args[arg_no].p;
459
460 fmt++;
461 ++arg_count;
462 if (abfd == NULL)
463 fprintf (fp, "%s generated", program_name);
464 else if (abfd->my_archive != NULL
465 && !bfd_is_thin_archive (abfd->my_archive))
466 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
467 abfd->filename);
468 else
469 fprintf (fp, "%s", abfd->filename);
470 }
471 else if (*fmt == 'I')
472 {
473 /* filename from a lang_input_statement_type */
474 lang_input_statement_type *i;
475
476 fmt++;
477 i = (lang_input_statement_type *) args[arg_no].p;
478 ++arg_count;
479 if (i->the_bfd != NULL
480 && i->the_bfd->my_archive != NULL
481 && !bfd_is_thin_archive (i->the_bfd->my_archive))
482 fprintf (fp, "(%s)%s", i->the_bfd->my_archive->filename,
483 i->local_sym_name);
484 else
485 fprintf (fp, "%s", i->filename);
486 }
487 else if (*fmt == 'R')
488 {
489 /* Print all that's interesting about a relent. */
490 arelent *relent = (arelent *) args[arg_no].p;
491
492 fmt++;
493 ++arg_count;
494 lfinfo (fp, "%s+0x%v (type %s)",
495 (*(relent->sym_ptr_ptr))->name,
496 relent->addend,
497 relent->howto->name);
498 }
499 else if (*fmt == 'S')
500 {
501 /* Print script file and linenumber. */
502 etree_type node;
503 etree_type *tp = (etree_type *) args[arg_no].p;
504
505 fmt++;
506 ++arg_count;
507 if (tp == NULL)
508 {
509 tp = &node;
510 tp->type.filename = ldlex_filename ();
511 tp->type.lineno = lineno;
512 }
513 if (tp->type.filename != NULL)
514 fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
515 }
516 else if (*fmt == 'T')
517 {
518 /* Symbol name. */
519 const char *name = (const char *) args[arg_no].p;
520
521 fmt++;
522 ++arg_count;
523 if (name == NULL || *name == 0)
524 {
525 fprintf (fp, _("no symbol"));
526 break;
527 }
528 else if (demangling)
529 {
530 char *demangled;
531
532 demangled = bfd_demangle (link_info.output_bfd, name,
533 DMGL_ANSI | DMGL_PARAMS);
534 if (demangled != NULL)
535 {
536 fprintf (fp, "%s", demangled);
537 free (demangled);
538 break;
539 }
540 }
541 fprintf (fp, "%s", name);
542 }
543 else
544 {
545 /* native (host) void* pointer, like printf */
546 fprintf (fp, "%p", args[arg_no].p);
547 ++arg_count;
548 }
549 break;
550
551 case 's':
552 /* arbitrary string, like printf */
553 fprintf (fp, "%s", (char *) args[arg_no].p);
554 ++arg_count;
555 break;
556
557 case 'd':
558 /* integer, like printf */
559 fprintf (fp, "%d", args[arg_no].i);
560 ++arg_count;
561 break;
562
563 case 'u':
564 /* unsigned integer, like printf */
565 fprintf (fp, "%u", args[arg_no].i);
566 ++arg_count;
567 break;
568
569 case 'l':
570 if (*fmt == 'd')
571 {
572 fprintf (fp, "%ld", args[arg_no].l);
573 ++arg_count;
574 ++fmt;
575 break;
576 }
577 else if (*fmt == 'u')
578 {
579 fprintf (fp, "%lu", args[arg_no].l);
580 ++arg_count;
581 ++fmt;
582 break;
583 }
584 /* Fallthru */
585
586 default:
587 fprintf (fp, "%%%c", fmt[-1]);
588 break;
589 }
590 }
591 }
592
593 if (is_warning && config.fatal_warnings)
594 config.make_executable = FALSE;
595
596 if (fatal)
597 xexit (1);
598 }
599
600 /* Format info message and print on stdout. */
601
602 /* (You would think this should be called just "info", but then you
603 would be hosed by LynxOS, which defines that name in its libc.) */
604
605 void
606 info_msg (const char *fmt, ...)
607 {
608 va_list arg;
609
610 va_start (arg, fmt);
611 vfinfo (stdout, fmt, arg, FALSE);
612 va_end (arg);
613 }
614
615 /* ('e' for error.) Format info message and print on stderr. */
616
617 void
618 einfo (const char *fmt, ...)
619 {
620 va_list arg;
621
622 fflush (stdout);
623 va_start (arg, fmt);
624 vfinfo (stderr, fmt, arg, TRUE);
625 va_end (arg);
626 fflush (stderr);
627 }
628
629 void
630 info_assert (const char *file, unsigned int line)
631 {
632 einfo (_("%F%P: internal error %s %d\n"), file, line);
633 }
634
635 /* ('m' for map) Format info message and print on map. */
636
637 void
638 minfo (const char *fmt, ...)
639 {
640 if (config.map_file != NULL)
641 {
642 va_list arg;
643
644 va_start (arg, fmt);
645 if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
646 {
647 /* Stash info about --as-needed shared libraries. Print
648 later so they don't appear intermingled with archive
649 library info. */
650 struct asneeded_minfo *m = xmalloc (sizeof *m);
651
652 m->next = NULL;
653 m->soname = va_arg (arg, const char *);
654 m->ref = va_arg (arg, bfd *);
655 m->name = va_arg (arg, const char *);
656 *asneeded_list_tail = m;
657 asneeded_list_tail = &m->next;
658 }
659 else
660 vfinfo (config.map_file, fmt, arg, FALSE);
661 va_end (arg);
662 }
663 }
664
665 void
666 lfinfo (FILE *file, const char *fmt, ...)
667 {
668 va_list arg;
669
670 va_start (arg, fmt);
671 vfinfo (file, fmt, arg, FALSE);
672 va_end (arg);
673 }
674 \f
675 /* Functions to print the link map. */
676
677 void
678 print_space (void)
679 {
680 fprintf (config.map_file, " ");
681 }
682
683 void
684 print_nl (void)
685 {
686 fprintf (config.map_file, "\n");
687 }
688
689 /* A more or less friendly abort message. In ld.h abort is defined to
690 call this function. */
691
692 void
693 ld_abort (const char *file, int line, const char *fn)
694 {
695 if (fn != NULL)
696 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
697 file, line, fn);
698 else
699 einfo (_("%P: internal error: aborting at %s:%d\n"),
700 file, line);
701 einfo (_("%F%P: please report this bug\n"));
702 xexit (1);
703 }
This page took 0.057896 seconds and 5 git commands to generate.