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