]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldmisc.c
Re: Get rid of fprintf_vma and sprintf_vma
[thirdparty/binutils-gdb.git] / ld / ldmisc.c
1 /* ldmisc.c
2 Copyright (C) 1991-2022 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 "ctf-api.h"
27 #include "safe-ctype.h"
28 #include "filenames.h"
29 #include "demangle.h"
30 #include <stdarg.h>
31 #include "ld.h"
32 #include "ldmisc.h"
33 #include "ldexp.h"
34 #include "ldlang.h"
35 #include <ldgram.h>
36 #include "ldlex.h"
37 #include "ldmain.h"
38 #include "ldfile.h"
39
40 /*
41 %% literal %
42 %C clever filename:linenumber with function
43 %D like %C, but no function name
44 %E current bfd error or errno
45 %F error is fatal
46 %G like %D, but only function name
47 %H like %C but in addition emit section+offset
48 %P print program name
49 %V hex bfd_vma
50 %W hex bfd_vma with 0x with no leading zeros taking up 10 spaces
51 %X no object output, fail return
52 %d integer, like printf
53 %ld long, like printf
54 %lu unsigned long, like printf
55 %p native (host) void* pointer, like printf
56 %pA section name from a section
57 %pB filename from a bfd
58 %pI filename from a lang_input_statement_type
59 %pR info about a relent
60 %pS print script file and linenumber from etree_type.
61 %pT symbol name
62 %pU print script file without linenumber from etree_type.
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, bool is_warning)
70 {
71 bool 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 char buf[32];
245 bfd_vma value;
246
247 value = args[arg_no].v;
248 ++arg_count;
249 bfd_sprintf_vma (link_info.output_bfd, buf, value);
250 fprintf (fp, "%s", buf);
251 }
252 break;
253
254 case 'v':
255 /* hex bfd_vma, no leading zeros */
256 {
257 uint64_t value = args[arg_no].v;
258 ++arg_count;
259 fprintf (fp, "%" PRIx64, value);
260 }
261 break;
262
263 case 'W':
264 /* hex bfd_vma with 0x with no leading zeroes taking up
265 10 spaces (including the 0x). */
266 {
267 char buf[32];
268 uint64_t value;
269
270 value = args[arg_no].v;
271 ++arg_count;
272 sprintf (buf, "0x%" PRIx64, value);
273 fprintf (fp, "%10s", buf);
274 }
275 break;
276
277 case 'F':
278 /* Error is fatal. */
279 fatal = true;
280 break;
281
282 case 'P':
283 /* Print program name. */
284 fprintf (fp, "%s", program_name);
285 break;
286
287 case 'E':
288 /* current bfd error or errno */
289 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
290 break;
291
292 case 'C':
293 case 'D':
294 case 'G':
295 case 'H':
296 /* Clever filename:linenumber with function name if possible.
297 The arguments are a BFD, a section, and an offset. */
298 {
299 static bfd *last_bfd;
300 static char *last_file;
301 static char *last_function;
302 bfd *abfd;
303 asection *section;
304 bfd_vma offset;
305 asymbol **asymbols = NULL;
306 const char *filename;
307 const char *functionname;
308 unsigned int linenumber;
309 bool discard_last;
310 bool done;
311 bfd_error_type last_bfd_error = bfd_get_error ();
312
313 abfd = args[arg_no].reladdr.abfd;
314 section = args[arg_no].reladdr.sec;
315 offset = args[arg_no].reladdr.off;
316 ++arg_count;
317
318 if (abfd != NULL)
319 {
320 if (!bfd_generic_link_read_symbols (abfd))
321 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
322
323 asymbols = bfd_get_outsymbols (abfd);
324 }
325
326 /* The GNU Coding Standard requires that error messages
327 be of the form:
328
329 source-file-name:lineno: message
330
331 We do not always have a line number available so if
332 we cannot find them we print out the section name and
333 offset instead. */
334 discard_last = true;
335 if (abfd != NULL
336 && bfd_find_nearest_line (abfd, section, asymbols, offset,
337 &filename, &functionname,
338 &linenumber))
339 {
340 if (functionname != NULL
341 && (fmt[-1] == 'C' || fmt[-1] == 'H'))
342 {
343 /* Detect the case where we are printing out a
344 message for the same function as the last
345 call to vinfo ("%C"). In this situation do
346 not print out the ABFD filename or the
347 function name again. Note - we do still
348 print out the source filename, as this will
349 allow programs that parse the linker's output
350 (eg emacs) to correctly locate multiple
351 errors in the same source file. */
352 if (last_bfd == NULL
353 || last_function == NULL
354 || last_bfd != abfd
355 || (last_file == NULL) != (filename == NULL)
356 || (filename != NULL
357 && filename_cmp (last_file, filename) != 0)
358 || strcmp (last_function, functionname) != 0)
359 {
360 lfinfo (fp, _("%pB: in function `%pT':\n"),
361 abfd, functionname);
362
363 last_bfd = abfd;
364 free (last_file);
365 last_file = NULL;
366 if (filename)
367 last_file = xstrdup (filename);
368 free (last_function);
369 last_function = xstrdup (functionname);
370 }
371 discard_last = false;
372 }
373 else
374 lfinfo (fp, "%pB:", abfd);
375
376 if (filename != NULL)
377 fprintf (fp, "%s:", filename);
378
379 done = fmt[-1] != 'H';
380 if (functionname != NULL && fmt[-1] == 'G')
381 lfinfo (fp, "%pT", functionname);
382 else if (filename != NULL && linenumber != 0)
383 fprintf (fp, "%u%s", linenumber, done ? "" : ":");
384 else
385 done = false;
386 }
387 else
388 {
389 lfinfo (fp, "%pB:", abfd);
390 done = false;
391 }
392 if (!done)
393 lfinfo (fp, "(%pA+0x%v)", section, offset);
394 bfd_set_error (last_bfd_error);
395
396 if (discard_last)
397 {
398 last_bfd = NULL;
399 free (last_file);
400 last_file = NULL;
401 free (last_function);
402 last_function = NULL;
403 }
404 }
405 break;
406
407 case 'p':
408 if (*fmt == 'A')
409 {
410 /* section name from a section */
411 asection *sec;
412 bfd *abfd;
413
414 fmt++;
415 sec = (asection *) args[arg_no].p;
416 ++arg_count;
417 fprintf (fp, "%s", sec->name);
418 abfd = sec->owner;
419 if (abfd != NULL)
420 {
421 const char *group = bfd_group_name (abfd, sec);
422 if (group != NULL)
423 fprintf (fp, "[%s]", group);
424 }
425 }
426 else if (*fmt == 'B')
427 {
428 /* filename from a bfd */
429 bfd *abfd = (bfd *) args[arg_no].p;
430
431 fmt++;
432 ++arg_count;
433 if (abfd == NULL)
434 fprintf (fp, "%s generated", program_name);
435 else if (abfd->my_archive != NULL
436 && !bfd_is_thin_archive (abfd->my_archive))
437 fprintf (fp, "%s(%s)",
438 bfd_get_filename (abfd->my_archive),
439 bfd_get_filename (abfd));
440 else
441 fprintf (fp, "%s", bfd_get_filename (abfd));
442 }
443 else if (*fmt == 'I')
444 {
445 /* filename from a lang_input_statement_type */
446 lang_input_statement_type *i;
447
448 fmt++;
449 i = (lang_input_statement_type *) args[arg_no].p;
450 ++arg_count;
451 if (i->the_bfd != NULL
452 && i->the_bfd->my_archive != NULL
453 && !bfd_is_thin_archive (i->the_bfd->my_archive))
454 fprintf (fp, "(%s)%s",
455 bfd_get_filename (i->the_bfd->my_archive),
456 i->local_sym_name);
457 else
458 fprintf (fp, "%s", i->filename);
459 }
460 else if (*fmt == 'R')
461 {
462 /* Print all that's interesting about a relent. */
463 arelent *relent = (arelent *) args[arg_no].p;
464
465 fmt++;
466 ++arg_count;
467 lfinfo (fp, "%s+0x%v (type %s)",
468 (*(relent->sym_ptr_ptr))->name,
469 relent->addend,
470 relent->howto->name);
471 }
472 else if (*fmt == 'S' || *fmt == 'U')
473 {
474 /* Print script file and perhaps the associated linenumber. */
475 etree_type node;
476 etree_type *tp = (etree_type *) args[arg_no].p;
477
478 fmt++;
479 ++arg_count;
480 if (tp == NULL)
481 {
482 tp = &node;
483 tp->type.filename = ldlex_filename ();
484 tp->type.lineno = lineno;
485 }
486 if (tp->type.filename != NULL && fmt[-1] == 'S')
487 fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
488 else if (tp->type.filename != NULL && fmt[-1] == 'U')
489 fprintf (fp, "%s", tp->type.filename);
490 }
491 else if (*fmt == 'T')
492 {
493 /* Symbol name. */
494 const char *name = (const char *) args[arg_no].p;
495
496 fmt++;
497 ++arg_count;
498 if (name == NULL || *name == 0)
499 {
500 fprintf (fp, _("no symbol"));
501 break;
502 }
503 else if (demangling)
504 {
505 char *demangled;
506
507 demangled = bfd_demangle (link_info.output_bfd, name,
508 DMGL_ANSI | DMGL_PARAMS);
509 if (demangled != NULL)
510 {
511 fprintf (fp, "%s", demangled);
512 free (demangled);
513 break;
514 }
515 }
516 fprintf (fp, "%s", name);
517 }
518 else
519 {
520 /* native (host) void* pointer, like printf */
521 fprintf (fp, "%p", args[arg_no].p);
522 ++arg_count;
523 }
524 break;
525
526 case 's':
527 /* arbitrary string, like printf */
528 fprintf (fp, "%s", (char *) args[arg_no].p);
529 ++arg_count;
530 break;
531
532 case 'd':
533 /* integer, like printf */
534 fprintf (fp, "%d", args[arg_no].i);
535 ++arg_count;
536 break;
537
538 case 'u':
539 /* unsigned integer, like printf */
540 fprintf (fp, "%u", args[arg_no].i);
541 ++arg_count;
542 break;
543
544 case 'l':
545 if (*fmt == 'd')
546 {
547 fprintf (fp, "%ld", args[arg_no].l);
548 ++arg_count;
549 ++fmt;
550 break;
551 }
552 else if (*fmt == 'u')
553 {
554 fprintf (fp, "%lu", args[arg_no].l);
555 ++arg_count;
556 ++fmt;
557 break;
558 }
559 /* Fallthru */
560
561 default:
562 fprintf (fp, "%%%c", fmt[-1]);
563 break;
564 }
565 }
566 }
567
568 if (is_warning && config.fatal_warnings)
569 config.make_executable = false;
570
571 if (fatal)
572 xexit (1);
573 }
574
575 /* Format info message and print on stdout. */
576
577 /* (You would think this should be called just "info", but then you
578 would be hosed by LynxOS, which defines that name in its libc.) */
579
580 void
581 info_msg (const char *fmt, ...)
582 {
583 va_list arg;
584
585 va_start (arg, fmt);
586 vfinfo (stdout, fmt, arg, false);
587 va_end (arg);
588 }
589
590 /* ('e' for error.) Format info message and print on stderr. */
591
592 void
593 einfo (const char *fmt, ...)
594 {
595 va_list arg;
596
597 fflush (stdout);
598 va_start (arg, fmt);
599 vfinfo (stderr, fmt, arg, true);
600 va_end (arg);
601 fflush (stderr);
602 }
603
604 void
605 info_assert (const char *file, unsigned int line)
606 {
607 einfo (_("%F%P: internal error %s %d\n"), file, line);
608 }
609
610 /* ('m' for map) Format info message and print on map. */
611
612 void
613 minfo (const char *fmt, ...)
614 {
615 if (config.map_file != NULL)
616 {
617 va_list arg;
618
619 va_start (arg, fmt);
620 if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
621 {
622 /* Stash info about --as-needed shared libraries. Print
623 later so they don't appear intermingled with archive
624 library info. */
625 struct asneeded_minfo *m = xmalloc (sizeof *m);
626
627 m->next = NULL;
628 m->soname = va_arg (arg, const char *);
629 m->ref = va_arg (arg, bfd *);
630 m->name = va_arg (arg, const char *);
631 *asneeded_list_tail = m;
632 asneeded_list_tail = &m->next;
633 }
634 else
635 vfinfo (config.map_file, fmt, arg, false);
636 va_end (arg);
637 }
638 }
639
640 void
641 lfinfo (FILE *file, const char *fmt, ...)
642 {
643 va_list arg;
644
645 va_start (arg, fmt);
646 vfinfo (file, fmt, arg, false);
647 va_end (arg);
648 }
649 \f
650 /* Functions to print the link map. */
651
652 void
653 print_spaces (int count)
654 {
655 fprintf (config.map_file, "%*s", count, "");
656 }
657
658 void
659 print_nl (void)
660 {
661 fprintf (config.map_file, "\n");
662 }
663
664 /* A more or less friendly abort message. In ld.h abort is defined to
665 call this function. */
666
667 void
668 ld_abort (const char *file, int line, const char *fn)
669 {
670 if (fn != NULL)
671 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
672 file, line, fn);
673 else
674 einfo (_("%P: internal error: aborting at %s:%d\n"),
675 file, line);
676 einfo (_("%F%P: please report this bug\n"));
677 xexit (1);
678 }