]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/ldmisc.c
* ld.texinfo: Document --fatal-warnings.
[thirdparty/binutils-gdb.git] / ld / ldmisc.c
CommitLineData
252b5132 1/* ldmisc.c
a2b64bed 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support.
5
6This file is part of GLD, the Gnu Linker.
7
8GLD is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GLD is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GLD; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libiberty.h"
26#include "demangle.h"
27
28#ifdef ANSI_PROTOTYPES
29#include <stdarg.h>
30#define USE_STDARG 1
31#else
32#include <varargs.h>
33#define USE_STDARG 0
34#endif
35
36#include "ld.h"
37#include "ldmisc.h"
38#include "ldexp.h"
39#include "ldlang.h"
40#include "ldgram.h"
41#include "ldlex.h"
42#include "ldmain.h"
43#include "ldfile.h"
44
45static void vfinfo PARAMS ((FILE *, const char *, va_list));
46
47/*
48 %% literal %
49 %F error is fatal
50 %P print program name
51 %S print script file and linenumber
52 %E current bfd error or errno
53 %I filename from a lang_input_statement_type
54 %B filename from a bfd
55 %T symbol name
56 %X no object output, fail return
57 %V hex bfd_vma
58 %v hex bfd_vma, no leading zeros
59 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
60 %C clever filename:linenumber with function
61 %D like %C, but no function name
62 %G like %D, but only function name
63 %R info about a relent
64 %s arbitrary string, like printf
65 %d integer, like printf
66 %u integer, like printf
67*/
68
69char *
70demangle (string)
71 const char *string;
72{
73 char *res;
74
75 if (output_bfd != NULL
76 && bfd_get_symbol_leading_char (output_bfd) == string[0])
77 ++string;
78
6d5e62f8
KH
79 /* This is a hack for better error reporting on XCOFF, or the MS PE
80 format. Xcoff has a single '.', while the NT PE for PPC has
81 '..'. So we remove all of them. */
82 while (string[0] == '.')
252b5132
RH
83 ++string;
84
85 res = cplus_demangle (string, DMGL_ANSI | DMGL_PARAMS);
86 return res ? res : xstrdup (string);
87}
88
89static void
90vfinfo (fp, fmt, arg)
91 FILE *fp;
92 const char *fmt;
93 va_list arg;
94{
95 boolean fatal = false;
96
97 while (*fmt != '\0')
98 {
6d5e62f8 99 while (*fmt != '%' && *fmt != '\0')
252b5132
RH
100 {
101 putc (*fmt, fp);
102 fmt++;
103 }
104
6d5e62f8 105 if (*fmt == '%')
252b5132 106 {
6d5e62f8
KH
107 fmt++;
108 switch (*fmt++)
252b5132
RH
109 {
110 default:
6d5e62f8 111 fprintf (fp, "%%%c", fmt[-1]);
252b5132
RH
112 break;
113
114 case '%':
115 /* literal % */
116 putc ('%', fp);
117 break;
118
119 case 'X':
120 /* no object output, fail return */
121 config.make_executable = false;
122 break;
123
124 case 'V':
125 /* hex bfd_vma */
126 {
127 bfd_vma value = va_arg (arg, bfd_vma);
128 fprintf_vma (fp, value);
129 }
130 break;
131
132 case 'v':
133 /* hex bfd_vma, no leading zeros */
134 {
135 char buf[100];
136 char *p = buf;
137 bfd_vma value = va_arg (arg, bfd_vma);
138 sprintf_vma (p, value);
139 while (*p == '0')
140 p++;
141 if (!*p)
142 p--;
143 fputs (p, fp);
144 }
145 break;
146
147 case 'W':
148 /* hex bfd_vma with 0x with no leading zeroes taking up
149 8 spaces. */
150 {
151 char buf[100];
152 bfd_vma value;
153 char *p;
154 int len;
155
156 value = va_arg (arg, bfd_vma);
157 sprintf_vma (buf, value);
158 for (p = buf; *p == '0'; ++p)
159 ;
160 if (*p == '\0')
161 --p;
162 len = strlen (p);
163 while (len < 8)
164 {
165 putc (' ', fp);
166 ++len;
167 }
168 fprintf (fp, "0x%s", p);
169 }
170 break;
171
172 case 'T':
173 /* Symbol name. */
174 {
175 const char *name = va_arg (arg, const char *);
176
177 if (name == (const char *) NULL || *name == 0)
178 fprintf (fp, _("no symbol"));
179 else if (! demangling)
180 fprintf (fp, "%s", name);
181 else
182 {
183 char *demangled;
184
185 demangled = demangle (name);
186 fprintf (fp, "%s", demangled);
187 free (demangled);
188 }
189 }
190 break;
191
192 case 'B':
193 /* filename from a bfd */
6d5e62f8 194 {
252b5132
RH
195 bfd *abfd = va_arg (arg, bfd *);
196 if (abfd->my_archive)
197 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
198 abfd->filename);
199 else
200 fprintf (fp, "%s", abfd->filename);
201 }
202 break;
203
204 case 'F':
6d5e62f8 205 /* Error is fatal. */
252b5132
RH
206 fatal = true;
207 break;
208
209 case 'P':
6d5e62f8 210 /* Print program name. */
252b5132
RH
211 fprintf (fp, "%s", program_name);
212 break;
213
214 case 'E':
215 /* current bfd error or errno */
305c7206 216 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
252b5132
RH
217 break;
218
219 case 'I':
220 /* filename from a lang_input_statement_type */
221 {
222 lang_input_statement_type *i;
223
224 i = va_arg (arg, lang_input_statement_type *);
225 if (bfd_my_archive (i->the_bfd) != NULL)
226 fprintf (fp, "(%s)",
227 bfd_get_filename (bfd_my_archive (i->the_bfd)));
228 fprintf (fp, "%s", i->local_sym_name);
229 if (bfd_my_archive (i->the_bfd) == NULL
230 && strcmp (i->local_sym_name, i->filename) != 0)
231 fprintf (fp, " (%s)", i->filename);
232 }
233 break;
234
235 case 'S':
6d5e62f8 236 /* Print script file and linenumber. */
252b5132
RH
237 if (parsing_defsym)
238 fprintf (fp, "--defsym %s", lex_string);
239 else if (ldfile_input_filename != NULL)
240 fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
241 else
242 fprintf (fp, _("built in linker script:%u"), lineno);
243 break;
244
245 case 'R':
6d5e62f8 246 /* Print all that's interesting about a relent. */
252b5132
RH
247 {
248 arelent *relent = va_arg (arg, arelent *);
6d5e62f8 249
252b5132
RH
250 lfinfo (fp, "%s+0x%v (type %s)",
251 (*(relent->sym_ptr_ptr))->name,
252 relent->addend,
253 relent->howto->name);
254 }
255 break;
6d5e62f8 256
252b5132
RH
257 case 'C':
258 case 'D':
259 case 'G':
260 /* Clever filename:linenumber with function name if possible,
261 or section name as a last resort. The arguments are a BFD,
262 a section, and an offset. */
263 {
264 static bfd *last_bfd;
265 static char *last_file = NULL;
266 static char *last_function = NULL;
267 bfd *abfd;
268 asection *section;
269 bfd_vma offset;
270 lang_input_statement_type *entry;
271 asymbol **asymbols;
272 const char *filename;
273 const char *functionname;
274 unsigned int linenumber;
275 boolean discard_last;
276
277 abfd = va_arg (arg, bfd *);
278 section = va_arg (arg, asection *);
279 offset = va_arg (arg, bfd_vma);
280
281 entry = (lang_input_statement_type *) abfd->usrdata;
282 if (entry != (lang_input_statement_type *) NULL
283 && entry->asymbols != (asymbol **) NULL)
284 asymbols = entry->asymbols;
285 else
286 {
287 long symsize;
288 long symbol_count;
289
290 symsize = bfd_get_symtab_upper_bound (abfd);
291 if (symsize < 0)
292 einfo (_("%B%F: could not read symbols\n"), abfd);
293 asymbols = (asymbol **) xmalloc (symsize);
294 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
295 if (symbol_count < 0)
296 einfo (_("%B%F: could not read symbols\n"), abfd);
297 if (entry != (lang_input_statement_type *) NULL)
298 {
299 entry->asymbols = asymbols;
300 entry->symbol_count = symbol_count;
301 }
302 }
303
304 discard_last = true;
305 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
306 &filename, &functionname,
307 &linenumber))
308 {
309 if (functionname != NULL && fmt[-1] == 'G')
310 {
311 lfinfo (fp, "%B:", abfd);
312 if (filename != NULL
313 && strcmp (filename, bfd_get_filename (abfd)) != 0)
314 fprintf (fp, "%s:", filename);
315 lfinfo (fp, "%T", functionname);
316 }
317 else if (functionname != NULL && fmt[-1] == 'C')
318 {
319 if (filename == (char *) NULL)
320 filename = abfd->filename;
321
322 if (last_bfd == NULL
323 || last_file == NULL
324 || last_function == NULL
325 || last_bfd != abfd
326 || strcmp (last_file, filename) != 0
327 || strcmp (last_function, functionname) != 0)
328 {
329 /* We use abfd->filename in this initial line,
330 in case filename is a .h file or something
331 similarly unhelpful. */
332 lfinfo (fp, _("%B: In function `%T':\n"),
333 abfd, functionname);
334
335 last_bfd = abfd;
336 if (last_file != NULL)
337 free (last_file);
338 last_file = buystring (filename);
339 if (last_function != NULL)
340 free (last_function);
341 last_function = buystring (functionname);
342 }
343 discard_last = false;
344 if (linenumber != 0)
345 fprintf (fp, "%s:%u", filename, linenumber);
346 else
347 lfinfo (fp, "%s(%s+0x%v)", filename, section->name,
348 offset);
349 }
350 else if (filename == NULL
351 || strcmp (filename, abfd->filename) == 0)
352 {
353 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name,
354 offset);
355 if (linenumber != 0)
356 lfinfo (fp, ":%u", linenumber);
357 }
6d5e62f8 358 else if (linenumber != 0)
252b5132
RH
359 lfinfo (fp, "%B:%s:%u", abfd, filename, linenumber);
360 else
361 lfinfo (fp, "%B(%s+0x%v):%s", abfd, section->name,
362 offset, filename);
363 }
364 else
365 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
366
367 if (discard_last)
368 {
369 last_bfd = NULL;
370 if (last_file != NULL)
371 {
372 free (last_file);
373 last_file = NULL;
374 }
375 if (last_function != NULL)
376 {
377 free (last_function);
378 last_function = NULL;
379 }
380 }
381 }
382 break;
6d5e62f8 383
252b5132
RH
384 case 's':
385 /* arbitrary string, like printf */
386 fprintf (fp, "%s", va_arg (arg, char *));
387 break;
388
389 case 'd':
390 /* integer, like printf */
391 fprintf (fp, "%d", va_arg (arg, int));
392 break;
393
394 case 'u':
395 /* unsigned integer, like printf */
396 fprintf (fp, "%u", va_arg (arg, unsigned int));
397 break;
398 }
399 }
400 }
401
7ce691ae
C
402 if (config.fatal_warnings)
403 config.make_executable = false;
404
6d5e62f8
KH
405 if (fatal == true)
406 xexit (1);
252b5132
RH
407}
408
6d5e62f8 409/* Format info message and print on stdout. */
252b5132
RH
410
411/* (You would think this should be called just "info", but then you
412 would hosed by LynxOS, which defines that name in its libc.) */
413
414void
415#if USE_STDARG
416info_msg (const char *fmt, ...)
417#else
418info_msg (va_alist)
419 va_dcl
420#endif
421{
422 va_list arg;
423
424#if ! USE_STDARG
425 const char *fmt;
426
427 va_start (arg);
428 fmt = va_arg (arg, const char *);
429#else
430 va_start (arg, fmt);
431#endif
432
433 vfinfo (stdout, fmt, arg);
434 va_end (arg);
435}
436
6d5e62f8 437/* ('e' for error.) Format info message and print on stderr. */
252b5132
RH
438
439void
440#if USE_STDARG
441einfo (const char *fmt, ...)
442#else
443einfo (va_alist)
444 va_dcl
445#endif
446{
447 va_list arg;
448
449#if ! USE_STDARG
450 const char *fmt;
451
452 va_start (arg);
453 fmt = va_arg (arg, const char *);
454#else
455 va_start (arg, fmt);
456#endif
457
458 vfinfo (stderr, fmt, arg);
459 va_end (arg);
460}
461
6d5e62f8 462void
252b5132
RH
463info_assert (file, line)
464 const char *file;
465 unsigned int line;
466{
467 einfo (_("%F%P: internal error %s %d\n"), file, line);
468}
469
470char *
471buystring (x)
472 CONST char *CONST x;
473{
6d5e62f8
KH
474 size_t l = strlen (x) + 1;
475 char *r = xmalloc (l);
476 memcpy (r, x, l);
252b5132
RH
477 return r;
478}
479
6d5e62f8 480/* ('m' for map) Format info message and print on map. */
252b5132
RH
481
482void
483#if USE_STDARG
484minfo (const char *fmt, ...)
485#else
486minfo (va_alist)
487 va_dcl
488#endif
489{
490 va_list arg;
491
492#if ! USE_STDARG
493 const char *fmt;
494 va_start (arg);
495 fmt = va_arg (arg, const char *);
496#else
497 va_start (arg, fmt);
498#endif
499
500 vfinfo (config.map_file, fmt, arg);
501 va_end (arg);
502}
503
504void
505#if USE_STDARG
506lfinfo (FILE *file, const char *fmt, ...)
507#else
508lfinfo (va_alist)
509 va_dcl
510#endif
511{
512 va_list arg;
513
514#if ! USE_STDARG
515 FILE *file;
516 const char *fmt;
517
518 va_start (arg);
519 file = va_arg (arg, FILE *);
520 fmt = va_arg (arg, const char *);
521#else
522 va_start (arg, fmt);
523#endif
524
525 vfinfo (file, fmt, arg);
526 va_end (arg);
527}
528\f
529/* Functions to print the link map. */
530
6d5e62f8 531void
252b5132
RH
532print_space ()
533{
534 fprintf (config.map_file, " ");
535}
536
6d5e62f8 537void
252b5132
RH
538print_nl ()
539{
540 fprintf (config.map_file, "\n");
541}
45455cdd
ILT
542
543/* A more or less friendly abort message. In ld.h abort is defined to
544 call this function. */
545
546void
547ld_abort (file, line, fn)
548 const char *file;
549 int line;
550 const char *fn;
551{
552 if (fn != NULL)
553 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
554 file, line, fn);
555 else
556 einfo (_("%P: internal error: aborting at %s line %d\n"),
557 file, line);
558 einfo (_("%P%F: please report this bug\n"));
559 xexit (1);
560}