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