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