]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldmisc.c
xtensa error message
[thirdparty/binutils-gdb.git] / ld / ldmisc.c
CommitLineData
252b5132 1/* ldmisc.c
2571583a 2 Copyright (C) 1991-2017 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 %A section name from a section
44 %B filename from a bfd
45 %C clever filename:linenumber with function
46 %D like %C, but no function name
47 %E current bfd error or errno
252b5132 48 %F error is fatal
d003868e 49 %G like %D, but only function name
270396f2 50 %H like %C but in addition emit section+offset
d003868e 51 %I filename from a lang_input_statement_type
252b5132 52 %P print program name
d003868e 53 %R info about a relent
dab69f68 54 %S print script file and linenumber from etree_type.
252b5132 55 %T symbol name
252b5132 56 %V hex bfd_vma
252b5132 57 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
d003868e 58 %X no object output, fail return
252b5132 59 %d integer, like printf
94b50910
AM
60 %ld long, like printf
61 %lu unsigned long, like printf
5d3236ee 62 %p native (host) void* pointer, like printf
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':
133 case 'A':
134 case 'B':
135 case 'I':
136 case 'S':
137 case 'R':
138 case 'p':
139 case 's':
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 }
252b5132 175
99847db8
AM
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;
252b5132
RH
203 while (*fmt != '\0')
204 {
23916fff 205 const char *str = fmt;
6d5e62f8 206 while (*fmt != '%' && *fmt != '\0')
23916fff
MF
207 fmt++;
208 if (fmt != str)
209 if (fwrite (str, 1, fmt - str, fp))
210 {
211 /* Ignore. */
212 }
252b5132 213
6d5e62f8 214 if (*fmt == '%')
252b5132 215 {
6d5e62f8 216 fmt++;
99847db8
AM
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
6d5e62f8 225 switch (*fmt++)
252b5132 226 {
99847db8
AM
227 case '\0':
228 --fmt;
229 /* Fall through. */
230
252b5132
RH
231 case '%':
232 /* literal % */
233 putc ('%', fp);
234 break;
235
236 case 'X':
237 /* no object output, fail return */
b34976b6 238 config.make_executable = FALSE;
252b5132
RH
239 break;
240
241 case 'V':
242 /* hex bfd_vma */
243 {
99847db8
AM
244 bfd_vma value = args[arg_no].v;
245 ++arg_count;
252b5132
RH
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;
99847db8
AM
255 bfd_vma value = args[arg_no].v;
256 ++arg_count;
252b5132
RH
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
1579bae1 268 8 spaces. */
252b5132
RH
269 {
270 char buf[100];
271 bfd_vma value;
272 char *p;
273 int len;
274
99847db8
AM
275 value = args[arg_no].v;
276 ++arg_count;
252b5132
RH
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 'T':
293 /* Symbol name. */
294 {
99847db8
AM
295 const char *name = (const char *) args[arg_no].p;
296 ++arg_count;
1579bae1 297 if (name == NULL || *name == 0)
73705ac3
AM
298 {
299 fprintf (fp, _("no symbol"));
300 break;
301 }
302 else if (demangling)
252b5132
RH
303 {
304 char *demangled;
305
f13a99db 306 demangled = bfd_demangle (link_info.output_bfd, name,
73705ac3
AM
307 DMGL_ANSI | DMGL_PARAMS);
308 if (demangled != NULL)
309 {
310 fprintf (fp, "%s", demangled);
311 free (demangled);
312 break;
313 }
252b5132 314 }
73705ac3 315 fprintf (fp, "%s", name);
252b5132
RH
316 }
317 break;
318
d003868e
AM
319 case 'A':
320 /* section name from a section */
321 {
99847db8
AM
322 asection *sec;
323 bfd *abfd;
d003868e
AM
324 const char *group = NULL;
325 struct coff_comdat_info *ci;
326
99847db8
AM
327 sec = (asection *) args[arg_no].p;
328 ++arg_count;
329 abfd = sec->owner;
d003868e
AM
330 fprintf (fp, "%s", sec->name);
331 if (abfd != NULL
332 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
333 && elf_next_in_group (sec) != NULL
334 && (sec->flags & SEC_GROUP) == 0)
335 group = elf_group_name (sec);
336 else if (abfd != NULL
337 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
338 && (ci = bfd_coff_get_comdat_section (sec->owner,
339 sec)) != NULL)
340 group = ci->name;
341 if (group != NULL)
342 fprintf (fp, "[%s]", group);
343 }
344 break;
345
252b5132
RH
346 case 'B':
347 /* filename from a bfd */
6d5e62f8 348 {
99847db8
AM
349 bfd *abfd = (bfd *) args[arg_no].p;
350 ++arg_count;
f2763b01 351 if (abfd == NULL)
e1fffbe6 352 fprintf (fp, "%s generated", program_name);
b0cffb47
AM
353 else if (abfd->my_archive != NULL
354 && !bfd_is_thin_archive (abfd->my_archive))
252b5132
RH
355 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
356 abfd->filename);
357 else
358 fprintf (fp, "%s", abfd->filename);
359 }
360 break;
361
362 case 'F':
6d5e62f8 363 /* Error is fatal. */
b34976b6 364 fatal = TRUE;
252b5132
RH
365 break;
366
367 case 'P':
6d5e62f8 368 /* Print program name. */
252b5132
RH
369 fprintf (fp, "%s", program_name);
370 break;
371
372 case 'E':
373 /* current bfd error or errno */
305c7206 374 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
252b5132
RH
375 break;
376
377 case 'I':
378 /* filename from a lang_input_statement_type */
379 {
380 lang_input_statement_type *i;
381
99847db8
AM
382 i = (lang_input_statement_type *) args[arg_no].p;
383 ++arg_count;
3860d2b4
AM
384 if (i->the_bfd->my_archive != NULL
385 && !bfd_is_thin_archive (i->the_bfd->my_archive))
252b5132 386 fprintf (fp, "(%s)",
3860d2b4 387 bfd_get_filename (i->the_bfd->my_archive));
252b5132 388 fprintf (fp, "%s", i->local_sym_name);
3860d2b4
AM
389 if ((i->the_bfd->my_archive == NULL
390 || bfd_is_thin_archive (i->the_bfd->my_archive))
42627821 391 && filename_cmp (i->local_sym_name, i->filename) != 0)
252b5132
RH
392 fprintf (fp, " (%s)", i->filename);
393 }
394 break;
395
396 case 'S':
6d5e62f8 397 /* Print script file and linenumber. */
dab69f68 398 {
39085894 399 etree_type node;
99847db8
AM
400 etree_type *tp = (etree_type *) args[arg_no].p;
401 ++arg_count;
dab69f68
AM
402 if (tp == NULL)
403 {
39085894 404 tp = &node;
dab69f68
AM
405 tp->type.filename = ldlex_filename ();
406 tp->type.lineno = lineno;
407 }
408 if (tp->type.filename != NULL)
409 fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
410 }
252b5132
RH
411 break;
412
413 case 'R':
6d5e62f8 414 /* Print all that's interesting about a relent. */
252b5132 415 {
99847db8
AM
416 arelent *relent = (arelent *) args[arg_no].p;
417 ++arg_count;
252b5132
RH
418 lfinfo (fp, "%s+0x%v (type %s)",
419 (*(relent->sym_ptr_ptr))->name,
420 relent->addend,
421 relent->howto->name);
422 }
423 break;
6d5e62f8 424
252b5132
RH
425 case 'C':
426 case 'D':
427 case 'G':
270396f2 428 case 'H':
5cfb2bb2
AM
429 /* Clever filename:linenumber with function name if possible.
430 The arguments are a BFD, a section, and an offset. */
252b5132
RH
431 {
432 static bfd *last_bfd;
befe814d
MR
433 static char *last_file;
434 static char *last_function;
252b5132
RH
435 bfd *abfd;
436 asection *section;
437 bfd_vma offset;
5c1d2f5f 438 asymbol **asymbols = NULL;
252b5132
RH
439 const char *filename;
440 const char *functionname;
441 unsigned int linenumber;
b34976b6 442 bfd_boolean discard_last;
270396f2 443 bfd_boolean done;
252b5132 444
99847db8
AM
445 abfd = args[arg_no].reladdr.abfd;
446 section = args[arg_no].reladdr.sec;
447 offset = args[arg_no].reladdr.off;
448 ++arg_count;
252b5132 449
5c1d2f5f 450 if (abfd != NULL)
252b5132 451 {
5c1d2f5f
AM
452 if (!bfd_generic_link_read_symbols (abfd))
453 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
454
455 asymbols = bfd_get_outsymbols (abfd);
252b5132
RH
456 }
457
e1fffbe6
AM
458 /* The GNU Coding Standard requires that error messages
459 be of the form:
e4492aa0 460
98d87ee7 461 source-file-name:lineno: message
5cfb2bb2 462
e1fffbe6
AM
463 We do not always have a line number available so if
464 we cannot find them we print out the section name and
270396f2 465 offset instead. */
b34976b6 466 discard_last = TRUE;
e1fffbe6
AM
467 if (abfd != NULL
468 && bfd_find_nearest_line (abfd, section, asymbols, offset,
469 &filename, &functionname,
470 &linenumber))
252b5132 471 {
270396f2
AM
472 if (functionname != NULL
473 && (fmt[-1] == 'C' || fmt[-1] == 'H'))
5cfb2bb2 474 {
e1fffbe6
AM
475 /* Detect the case where we are printing out a
476 message for the same function as the last
477 call to vinfo ("%C"). In this situation do
478 not print out the ABFD filename or the
479 function name again. Note - we do still
480 print out the source filename, as this will
481 allow programs that parse the linker's output
482 (eg emacs) to correctly locate multiple
483 errors in the same source file. */
252b5132 484 if (last_bfd == NULL
252b5132
RH
485 || last_function == NULL
486 || last_bfd != abfd
ebf0b03c 487 || (last_file == NULL) != (filename == NULL)
5cfb2bb2 488 || (filename != NULL
42627821 489 && filename_cmp (last_file, filename) != 0)
252b5132
RH
490 || strcmp (last_function, functionname) != 0)
491 {
a1c16379 492 lfinfo (fp, _("%B: In function `%T':\n"),
98d87ee7 493 abfd, functionname);
252b5132
RH
494
495 last_bfd = abfd;
496 if (last_file != NULL)
497 free (last_file);
5cfb2bb2
AM
498 last_file = NULL;
499 if (filename)
500 last_file = xstrdup (filename);
252b5132
RH
501 if (last_function != NULL)
502 free (last_function);
d1b2b2dc 503 last_function = xstrdup (functionname);
252b5132 504 }
b34976b6 505 discard_last = FALSE;
252b5132 506 }
98d87ee7 507 else
a1c16379 508 lfinfo (fp, "%B:", abfd);
5cfb2bb2
AM
509
510 if (filename != NULL)
a1c16379 511 fprintf (fp, "%s:", filename);
5cfb2bb2 512
270396f2 513 done = fmt[-1] != 'H';
5cfb2bb2 514 if (functionname != NULL && fmt[-1] == 'G')
a1c16379
JJ
515 lfinfo (fp, "%T", functionname);
516 else if (filename != NULL && linenumber != 0)
18ff9b9b 517 fprintf (fp, "%u%s", linenumber, done ? "" : ":");
a1c16379 518 else
270396f2 519 done = FALSE;
252b5132 520 }
98d87ee7 521 else
270396f2
AM
522 {
523 lfinfo (fp, "%B:", abfd);
524 done = FALSE;
525 }
526 if (!done)
527 lfinfo (fp, "(%A+0x%v)", section, offset);
252b5132
RH
528
529 if (discard_last)
530 {
531 last_bfd = NULL;
532 if (last_file != NULL)
533 {
534 free (last_file);
535 last_file = NULL;
536 }
537 if (last_function != NULL)
538 {
539 free (last_function);
540 last_function = NULL;
541 }
542 }
543 }
544 break;
6d5e62f8 545
5d3236ee
DK
546 case 'p':
547 /* native (host) void* pointer, like printf */
99847db8
AM
548 fprintf (fp, "%p", args[arg_no].p);
549 ++arg_count;
5d3236ee
DK
550 break;
551
252b5132
RH
552 case 's':
553 /* arbitrary string, like printf */
99847db8
AM
554 fprintf (fp, "%s", (char *) args[arg_no].p);
555 ++arg_count;
252b5132
RH
556 break;
557
558 case 'd':
559 /* integer, like printf */
99847db8
AM
560 fprintf (fp, "%d", args[arg_no].i);
561 ++arg_count;
252b5132
RH
562 break;
563
564 case 'u':
565 /* unsigned integer, like printf */
99847db8
AM
566 fprintf (fp, "%u", args[arg_no].i);
567 ++arg_count;
252b5132 568 break;
94b50910
AM
569
570 case 'l':
571 if (*fmt == 'd')
572 {
99847db8
AM
573 fprintf (fp, "%ld", args[arg_no].l);
574 ++arg_count;
94b50910
AM
575 ++fmt;
576 break;
577 }
578 else if (*fmt == 'u')
579 {
99847db8
AM
580 fprintf (fp, "%lu", args[arg_no].l);
581 ++arg_count;
94b50910
AM
582 ++fmt;
583 break;
584 }
370dfff4 585 /* Fallthru */
94b50910
AM
586
587 default:
588 fprintf (fp, "%%%c", fmt[-1]);
589 break;
252b5132
RH
590 }
591 }
592 }
593
59ef2528 594 if (is_warning && config.fatal_warnings)
b34976b6 595 config.make_executable = FALSE;
7ce691ae 596
b34976b6 597 if (fatal)
6d5e62f8 598 xexit (1);
252b5132
RH
599}
600
6d5e62f8 601/* Format info message and print on stdout. */
252b5132
RH
602
603/* (You would think this should be called just "info", but then you
1579bae1 604 would be hosed by LynxOS, which defines that name in its libc.) */
252b5132
RH
605
606void
1579bae1 607info_msg (const char *fmt, ...)
252b5132 608{
1579bae1 609 va_list arg;
252b5132 610
1579bae1 611 va_start (arg, fmt);
59ef2528 612 vfinfo (stdout, fmt, arg, FALSE);
1579bae1 613 va_end (arg);
252b5132
RH
614}
615
6d5e62f8 616/* ('e' for error.) Format info message and print on stderr. */
252b5132
RH
617
618void
1579bae1 619einfo (const char *fmt, ...)
252b5132 620{
1579bae1 621 va_list arg;
252b5132 622
e922bcab 623 fflush (stdout);
1579bae1 624 va_start (arg, fmt);
59ef2528 625 vfinfo (stderr, fmt, arg, TRUE);
1579bae1 626 va_end (arg);
e922bcab 627 fflush (stderr);
252b5132
RH
628}
629
6d5e62f8 630void
1579bae1 631info_assert (const char *file, unsigned int line)
252b5132
RH
632{
633 einfo (_("%F%P: internal error %s %d\n"), file, line);
634}
635
6d5e62f8 636/* ('m' for map) Format info message and print on map. */
252b5132
RH
637
638void
1579bae1 639minfo (const char *fmt, ...)
252b5132 640{
49fa1e15
AM
641 if (config.map_file != NULL)
642 {
643 va_list arg;
252b5132 644
49fa1e15 645 va_start (arg, fmt);
16e4ecc0
AM
646 if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
647 {
648 /* Stash info about --as-needed shared libraries. Print
649 later so they don't appear intermingled with archive
650 library info. */
651 struct asneeded_minfo *m = xmalloc (sizeof *m);
652
653 m->next = NULL;
654 m->soname = va_arg (arg, const char *);
655 m->ref = va_arg (arg, bfd *);
656 m->name = va_arg (arg, const char *);
657 *asneeded_list_tail = m;
658 asneeded_list_tail = &m->next;
659 }
660 else
661 vfinfo (config.map_file, fmt, arg, FALSE);
49fa1e15
AM
662 va_end (arg);
663 }
252b5132
RH
664}
665
666void
1579bae1 667lfinfo (FILE *file, const char *fmt, ...)
252b5132 668{
1579bae1 669 va_list arg;
252b5132 670
1579bae1 671 va_start (arg, fmt);
59ef2528 672 vfinfo (file, fmt, arg, FALSE);
1579bae1 673 va_end (arg);
252b5132
RH
674}
675\f
676/* Functions to print the link map. */
677
6d5e62f8 678void
1579bae1 679print_space (void)
252b5132
RH
680{
681 fprintf (config.map_file, " ");
682}
683
6d5e62f8 684void
1579bae1 685print_nl (void)
252b5132
RH
686{
687 fprintf (config.map_file, "\n");
688}
45455cdd
ILT
689
690/* A more or less friendly abort message. In ld.h abort is defined to
691 call this function. */
692
693void
1579bae1 694ld_abort (const char *file, int line, const char *fn)
45455cdd
ILT
695{
696 if (fn != NULL)
7ac01895 697 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
45455cdd
ILT
698 file, line, fn);
699 else
7ac01895 700 einfo (_("%P: internal error: aborting at %s:%d\n"),
45455cdd
ILT
701 file, line);
702 einfo (_("%P%F: please report this bug\n"));
703 xexit (1);
704}