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