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