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