Add separate debug info file section
[deliverable/binutils-gdb.git] / ld / ldmisc.c
CommitLineData
252b5132 1/* ldmisc.c
6f2750fe 2 Copyright (C) 1991-2016 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"
42627821 26#include "filenames.h"
252b5132 27#include "demangle.h"
252b5132 28#include <stdarg.h>
252b5132
RH
29#include "ld.h"
30#include "ldmisc.h"
31#include "ldexp.h"
32#include "ldlang.h"
df2a7313 33#include <ldgram.h>
252b5132
RH
34#include "ldlex.h"
35#include "ldmain.h"
36#include "ldfile.h"
d003868e 37#include "elf-bfd.h"
f4943d82 38#include "coff-bfd.h"
252b5132 39
252b5132
RH
40/*
41 %% literal %
d003868e
AM
42 %A section name from a section
43 %B filename from a bfd
44 %C clever filename:linenumber with function
45 %D like %C, but no function name
46 %E current bfd error or errno
252b5132 47 %F error is fatal
d003868e 48 %G like %D, but only function name
270396f2 49 %H like %C but in addition emit section+offset
d003868e 50 %I filename from a lang_input_statement_type
252b5132 51 %P print program name
d003868e 52 %R info about a relent
dab69f68 53 %S print script file and linenumber from etree_type.
252b5132 54 %T symbol name
252b5132 55 %V hex bfd_vma
252b5132 56 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
d003868e 57 %X no object output, fail return
252b5132 58 %d integer, like printf
94b50910
AM
59 %ld long, like printf
60 %lu unsigned long, like printf
5d3236ee 61 %p native (host) void* pointer, like printf
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
59ef2528 68vfinfo (FILE *fp, const char *fmt, va_list arg, bfd_boolean is_warning)
252b5132 69{
b34976b6 70 bfd_boolean fatal = FALSE;
252b5132
RH
71
72 while (*fmt != '\0')
73 {
23916fff 74 const char *str = fmt;
6d5e62f8 75 while (*fmt != '%' && *fmt != '\0')
23916fff
MF
76 fmt++;
77 if (fmt != str)
78 if (fwrite (str, 1, fmt - str, fp))
79 {
80 /* Ignore. */
81 }
252b5132 82
6d5e62f8 83 if (*fmt == '%')
252b5132 84 {
6d5e62f8
KH
85 fmt++;
86 switch (*fmt++)
252b5132 87 {
252b5132
RH
88 case '%':
89 /* literal % */
90 putc ('%', fp);
91 break;
92
93 case 'X':
94 /* no object output, fail return */
b34976b6 95 config.make_executable = FALSE;
252b5132
RH
96 break;
97
98 case 'V':
99 /* hex bfd_vma */
100 {
101 bfd_vma value = va_arg (arg, bfd_vma);
102 fprintf_vma (fp, value);
103 }
104 break;
105
106 case 'v':
107 /* hex bfd_vma, no leading zeros */
108 {
109 char buf[100];
110 char *p = buf;
111 bfd_vma value = va_arg (arg, bfd_vma);
112 sprintf_vma (p, value);
113 while (*p == '0')
114 p++;
115 if (!*p)
116 p--;
117 fputs (p, fp);
118 }
119 break;
120
121 case 'W':
122 /* hex bfd_vma with 0x with no leading zeroes taking up
1579bae1 123 8 spaces. */
252b5132
RH
124 {
125 char buf[100];
126 bfd_vma value;
127 char *p;
128 int len;
129
130 value = va_arg (arg, bfd_vma);
131 sprintf_vma (buf, value);
132 for (p = buf; *p == '0'; ++p)
133 ;
134 if (*p == '\0')
135 --p;
136 len = strlen (p);
137 while (len < 8)
138 {
139 putc (' ', fp);
140 ++len;
141 }
142 fprintf (fp, "0x%s", p);
143 }
144 break;
145
146 case 'T':
147 /* Symbol name. */
148 {
149 const char *name = va_arg (arg, const char *);
150
1579bae1 151 if (name == NULL || *name == 0)
73705ac3
AM
152 {
153 fprintf (fp, _("no symbol"));
154 break;
155 }
156 else if (demangling)
252b5132
RH
157 {
158 char *demangled;
159
f13a99db 160 demangled = bfd_demangle (link_info.output_bfd, name,
73705ac3
AM
161 DMGL_ANSI | DMGL_PARAMS);
162 if (demangled != NULL)
163 {
164 fprintf (fp, "%s", demangled);
165 free (demangled);
166 break;
167 }
252b5132 168 }
73705ac3 169 fprintf (fp, "%s", name);
252b5132
RH
170 }
171 break;
172
d003868e
AM
173 case 'A':
174 /* section name from a section */
175 {
176 asection *sec = va_arg (arg, asection *);
177 bfd *abfd = sec->owner;
178 const char *group = NULL;
179 struct coff_comdat_info *ci;
180
181 fprintf (fp, "%s", sec->name);
182 if (abfd != NULL
183 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
184 && elf_next_in_group (sec) != NULL
185 && (sec->flags & SEC_GROUP) == 0)
186 group = elf_group_name (sec);
187 else if (abfd != NULL
188 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
189 && (ci = bfd_coff_get_comdat_section (sec->owner,
190 sec)) != NULL)
191 group = ci->name;
192 if (group != NULL)
193 fprintf (fp, "[%s]", group);
194 }
195 break;
196
252b5132
RH
197 case 'B':
198 /* filename from a bfd */
6d5e62f8 199 {
252b5132 200 bfd *abfd = va_arg (arg, bfd *);
f2763b01
NC
201
202 if (abfd == NULL)
e1fffbe6 203 fprintf (fp, "%s generated", program_name);
b0cffb47
AM
204 else if (abfd->my_archive != NULL
205 && !bfd_is_thin_archive (abfd->my_archive))
252b5132
RH
206 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
207 abfd->filename);
208 else
209 fprintf (fp, "%s", abfd->filename);
210 }
211 break;
212
213 case 'F':
6d5e62f8 214 /* Error is fatal. */
b34976b6 215 fatal = TRUE;
252b5132
RH
216 break;
217
218 case 'P':
6d5e62f8 219 /* Print program name. */
252b5132
RH
220 fprintf (fp, "%s", program_name);
221 break;
222
223 case 'E':
224 /* current bfd error or errno */
305c7206 225 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
252b5132
RH
226 break;
227
228 case 'I':
229 /* filename from a lang_input_statement_type */
230 {
231 lang_input_statement_type *i;
232
233 i = va_arg (arg, lang_input_statement_type *);
3860d2b4
AM
234 if (i->the_bfd->my_archive != NULL
235 && !bfd_is_thin_archive (i->the_bfd->my_archive))
252b5132 236 fprintf (fp, "(%s)",
3860d2b4 237 bfd_get_filename (i->the_bfd->my_archive));
252b5132 238 fprintf (fp, "%s", i->local_sym_name);
3860d2b4
AM
239 if ((i->the_bfd->my_archive == NULL
240 || bfd_is_thin_archive (i->the_bfd->my_archive))
42627821 241 && filename_cmp (i->local_sym_name, i->filename) != 0)
252b5132
RH
242 fprintf (fp, " (%s)", i->filename);
243 }
244 break;
245
246 case 'S':
6d5e62f8 247 /* Print script file and linenumber. */
dab69f68 248 {
39085894 249 etree_type node;
dab69f68
AM
250 etree_type *tp = va_arg (arg, etree_type *);
251
252 if (tp == NULL)
253 {
39085894 254 tp = &node;
dab69f68
AM
255 tp->type.filename = ldlex_filename ();
256 tp->type.lineno = lineno;
257 }
258 if (tp->type.filename != NULL)
259 fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
260 }
252b5132
RH
261 break;
262
263 case 'R':
6d5e62f8 264 /* Print all that's interesting about a relent. */
252b5132
RH
265 {
266 arelent *relent = va_arg (arg, arelent *);
6d5e62f8 267
252b5132
RH
268 lfinfo (fp, "%s+0x%v (type %s)",
269 (*(relent->sym_ptr_ptr))->name,
270 relent->addend,
271 relent->howto->name);
272 }
273 break;
6d5e62f8 274
252b5132
RH
275 case 'C':
276 case 'D':
277 case 'G':
270396f2 278 case 'H':
5cfb2bb2
AM
279 /* Clever filename:linenumber with function name if possible.
280 The arguments are a BFD, a section, and an offset. */
252b5132
RH
281 {
282 static bfd *last_bfd;
283 static char *last_file = NULL;
284 static char *last_function = NULL;
285 bfd *abfd;
286 asection *section;
287 bfd_vma offset;
5c1d2f5f 288 asymbol **asymbols = NULL;
252b5132
RH
289 const char *filename;
290 const char *functionname;
291 unsigned int linenumber;
b34976b6 292 bfd_boolean discard_last;
270396f2 293 bfd_boolean done;
252b5132
RH
294
295 abfd = va_arg (arg, bfd *);
296 section = va_arg (arg, asection *);
297 offset = va_arg (arg, bfd_vma);
298
5c1d2f5f 299 if (abfd != NULL)
252b5132 300 {
5c1d2f5f
AM
301 if (!bfd_generic_link_read_symbols (abfd))
302 einfo (_("%B%F: could not read symbols: %E\n"), abfd);
303
304 asymbols = bfd_get_outsymbols (abfd);
252b5132
RH
305 }
306
e1fffbe6
AM
307 /* The GNU Coding Standard requires that error messages
308 be of the form:
e4492aa0 309
98d87ee7 310 source-file-name:lineno: message
5cfb2bb2 311
e1fffbe6
AM
312 We do not always have a line number available so if
313 we cannot find them we print out the section name and
270396f2 314 offset instead. */
b34976b6 315 discard_last = TRUE;
e1fffbe6
AM
316 if (abfd != NULL
317 && bfd_find_nearest_line (abfd, section, asymbols, offset,
318 &filename, &functionname,
319 &linenumber))
252b5132 320 {
270396f2
AM
321 if (functionname != NULL
322 && (fmt[-1] == 'C' || fmt[-1] == 'H'))
5cfb2bb2 323 {
e1fffbe6
AM
324 /* Detect the case where we are printing out a
325 message for the same function as the last
326 call to vinfo ("%C"). In this situation do
327 not print out the ABFD filename or the
328 function name again. Note - we do still
329 print out the source filename, as this will
330 allow programs that parse the linker's output
331 (eg emacs) to correctly locate multiple
332 errors in the same source file. */
252b5132
RH
333 if (last_bfd == NULL
334 || last_file == NULL
335 || last_function == NULL
336 || last_bfd != abfd
5cfb2bb2 337 || (filename != NULL
42627821 338 && filename_cmp (last_file, filename) != 0)
252b5132
RH
339 || strcmp (last_function, functionname) != 0)
340 {
a1c16379 341 lfinfo (fp, _("%B: In function `%T':\n"),
98d87ee7 342 abfd, functionname);
252b5132
RH
343
344 last_bfd = abfd;
345 if (last_file != NULL)
346 free (last_file);
5cfb2bb2
AM
347 last_file = NULL;
348 if (filename)
349 last_file = xstrdup (filename);
252b5132
RH
350 if (last_function != NULL)
351 free (last_function);
d1b2b2dc 352 last_function = xstrdup (functionname);
252b5132 353 }
b34976b6 354 discard_last = FALSE;
252b5132 355 }
98d87ee7 356 else
a1c16379 357 lfinfo (fp, "%B:", abfd);
5cfb2bb2
AM
358
359 if (filename != NULL)
a1c16379 360 fprintf (fp, "%s:", filename);
5cfb2bb2 361
270396f2 362 done = fmt[-1] != 'H';
5cfb2bb2 363 if (functionname != NULL && fmt[-1] == 'G')
a1c16379
JJ
364 lfinfo (fp, "%T", functionname);
365 else if (filename != NULL && linenumber != 0)
18ff9b9b 366 fprintf (fp, "%u%s", linenumber, done ? "" : ":");
a1c16379 367 else
270396f2 368 done = FALSE;
252b5132 369 }
98d87ee7 370 else
270396f2
AM
371 {
372 lfinfo (fp, "%B:", abfd);
373 done = FALSE;
374 }
375 if (!done)
376 lfinfo (fp, "(%A+0x%v)", section, offset);
252b5132
RH
377
378 if (discard_last)
379 {
380 last_bfd = NULL;
381 if (last_file != NULL)
382 {
383 free (last_file);
384 last_file = NULL;
385 }
386 if (last_function != NULL)
387 {
388 free (last_function);
389 last_function = NULL;
390 }
391 }
392 }
393 break;
6d5e62f8 394
5d3236ee
DK
395 case 'p':
396 /* native (host) void* pointer, like printf */
397 fprintf (fp, "%p", va_arg (arg, void *));
398 break;
399
252b5132
RH
400 case 's':
401 /* arbitrary string, like printf */
402 fprintf (fp, "%s", va_arg (arg, char *));
403 break;
404
405 case 'd':
406 /* integer, like printf */
407 fprintf (fp, "%d", va_arg (arg, int));
408 break;
409
410 case 'u':
411 /* unsigned integer, like printf */
412 fprintf (fp, "%u", va_arg (arg, unsigned int));
413 break;
94b50910
AM
414
415 case 'l':
416 if (*fmt == 'd')
417 {
418 fprintf (fp, "%ld", va_arg (arg, long));
419 ++fmt;
420 break;
421 }
422 else if (*fmt == 'u')
423 {
424 fprintf (fp, "%lu", va_arg (arg, unsigned long));
425 ++fmt;
426 break;
427 }
428 /* Fall thru */
429
430 default:
431 fprintf (fp, "%%%c", fmt[-1]);
432 break;
252b5132
RH
433 }
434 }
435 }
436
59ef2528 437 if (is_warning && config.fatal_warnings)
b34976b6 438 config.make_executable = FALSE;
7ce691ae 439
b34976b6 440 if (fatal)
6d5e62f8 441 xexit (1);
252b5132
RH
442}
443
6d5e62f8 444/* Format info message and print on stdout. */
252b5132
RH
445
446/* (You would think this should be called just "info", but then you
1579bae1 447 would be hosed by LynxOS, which defines that name in its libc.) */
252b5132
RH
448
449void
1579bae1 450info_msg (const char *fmt, ...)
252b5132 451{
1579bae1 452 va_list arg;
252b5132 453
1579bae1 454 va_start (arg, fmt);
59ef2528 455 vfinfo (stdout, fmt, arg, FALSE);
1579bae1 456 va_end (arg);
252b5132
RH
457}
458
6d5e62f8 459/* ('e' for error.) Format info message and print on stderr. */
252b5132
RH
460
461void
1579bae1 462einfo (const char *fmt, ...)
252b5132 463{
1579bae1 464 va_list arg;
252b5132 465
e922bcab 466 fflush (stdout);
1579bae1 467 va_start (arg, fmt);
59ef2528 468 vfinfo (stderr, fmt, arg, TRUE);
1579bae1 469 va_end (arg);
e922bcab 470 fflush (stderr);
252b5132
RH
471}
472
6d5e62f8 473void
1579bae1 474info_assert (const char *file, unsigned int line)
252b5132
RH
475{
476 einfo (_("%F%P: internal error %s %d\n"), file, line);
477}
478
6d5e62f8 479/* ('m' for map) Format info message and print on map. */
252b5132
RH
480
481void
1579bae1 482minfo (const char *fmt, ...)
252b5132 483{
49fa1e15
AM
484 if (config.map_file != NULL)
485 {
486 va_list arg;
252b5132 487
49fa1e15 488 va_start (arg, fmt);
16e4ecc0
AM
489 if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
490 {
491 /* Stash info about --as-needed shared libraries. Print
492 later so they don't appear intermingled with archive
493 library info. */
494 struct asneeded_minfo *m = xmalloc (sizeof *m);
495
496 m->next = NULL;
497 m->soname = va_arg (arg, const char *);
498 m->ref = va_arg (arg, bfd *);
499 m->name = va_arg (arg, const char *);
500 *asneeded_list_tail = m;
501 asneeded_list_tail = &m->next;
502 }
503 else
504 vfinfo (config.map_file, fmt, arg, FALSE);
49fa1e15
AM
505 va_end (arg);
506 }
252b5132
RH
507}
508
509void
1579bae1 510lfinfo (FILE *file, const char *fmt, ...)
252b5132 511{
1579bae1 512 va_list arg;
252b5132 513
1579bae1 514 va_start (arg, fmt);
59ef2528 515 vfinfo (file, fmt, arg, FALSE);
1579bae1 516 va_end (arg);
252b5132
RH
517}
518\f
519/* Functions to print the link map. */
520
6d5e62f8 521void
1579bae1 522print_space (void)
252b5132
RH
523{
524 fprintf (config.map_file, " ");
525}
526
6d5e62f8 527void
1579bae1 528print_nl (void)
252b5132
RH
529{
530 fprintf (config.map_file, "\n");
531}
45455cdd
ILT
532
533/* A more or less friendly abort message. In ld.h abort is defined to
534 call this function. */
535
536void
1579bae1 537ld_abort (const char *file, int line, const char *fn)
45455cdd
ILT
538{
539 if (fn != NULL)
7ac01895 540 einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
45455cdd
ILT
541 file, line, fn);
542 else
7ac01895 543 einfo (_("%P: internal error: aborting at %s:%d\n"),
45455cdd
ILT
544 file, line);
545 einfo (_("%P%F: please report this bug\n"));
546 xexit (1);
547}
This page took 0.83449 seconds and 4 git commands to generate.