Add DLX target
[binutils.git] / ld / ldmisc.c
blob42adceeac3eecfa5eafae5f3ddaf18e90a7a4507
1 /* ldmisc.c
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2002
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support.
7 This file is part of GLD, the Gnu Linker.
9 GLD is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GLD is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libiberty.h"
27 #include "demangle.h"
29 #ifdef ANSI_PROTOTYPES
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
35 #include "ld.h"
36 #include "ldmisc.h"
37 #include "ldexp.h"
38 #include "ldlang.h"
39 #include "ldgram.h"
40 #include "ldlex.h"
41 #include "ldmain.h"
42 #include "ldfile.h"
44 static void vfinfo PARAMS ((FILE *, const char *, va_list));
47 %% literal %
48 %F error is fatal
49 %P print program name
50 %S print script file and linenumber
51 %E current bfd error or errno
52 %I filename from a lang_input_statement_type
53 %B filename from a bfd
54 %T symbol name
55 %X no object output, fail return
56 %V hex bfd_vma
57 %v hex bfd_vma, no leading zeros
58 %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
59 %C clever filename:linenumber with function
60 %D like %C, but no function name
61 %G like %D, but only function name
62 %R info about a relent
63 %s arbitrary string, like printf
64 %d integer, like printf
65 %u integer, like printf
68 char *
69 demangle (string)
70 const char *string;
72 char *res;
73 const char *p;
75 if (output_bfd != NULL
76 && bfd_get_symbol_leading_char (output_bfd) == string[0])
77 ++string;
79 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
80 or the MS PE format. These formats have a number of leading '.'s
81 on at least some symbols, so we remove all dots. */
82 p = string;
83 while (*p == '.')
84 ++p;
86 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
87 return res ? res : xstrdup (string);
90 static void
91 vfinfo (fp, fmt, arg)
92 FILE *fp;
93 const char *fmt;
94 va_list arg;
96 boolean fatal = false;
98 while (*fmt != '\0')
100 while (*fmt != '%' && *fmt != '\0')
102 putc (*fmt, fp);
103 fmt++;
106 if (*fmt == '%')
108 fmt++;
109 switch (*fmt++)
111 default:
112 fprintf (fp, "%%%c", fmt[-1]);
113 break;
115 case '%':
116 /* literal % */
117 putc ('%', fp);
118 break;
120 case 'X':
121 /* no object output, fail return */
122 config.make_executable = false;
123 break;
125 case 'V':
126 /* hex bfd_vma */
128 bfd_vma value = va_arg (arg, bfd_vma);
129 fprintf_vma (fp, value);
131 break;
133 case 'v':
134 /* hex bfd_vma, no leading zeros */
136 char buf[100];
137 char *p = buf;
138 bfd_vma value = va_arg (arg, bfd_vma);
139 sprintf_vma (p, value);
140 while (*p == '0')
141 p++;
142 if (!*p)
143 p--;
144 fputs (p, fp);
146 break;
148 case 'W':
149 /* hex bfd_vma with 0x with no leading zeroes taking up
150 8 spaces. */
152 char buf[100];
153 bfd_vma value;
154 char *p;
155 int len;
157 value = va_arg (arg, bfd_vma);
158 sprintf_vma (buf, value);
159 for (p = buf; *p == '0'; ++p)
161 if (*p == '\0')
162 --p;
163 len = strlen (p);
164 while (len < 8)
166 putc (' ', fp);
167 ++len;
169 fprintf (fp, "0x%s", p);
171 break;
173 case 'T':
174 /* Symbol name. */
176 const char *name = va_arg (arg, const char *);
178 if (name == (const char *) NULL || *name == 0)
179 fprintf (fp, _("no symbol"));
180 else if (! demangling)
181 fprintf (fp, "%s", name);
182 else
184 char *demangled;
186 demangled = demangle (name);
187 fprintf (fp, "%s", demangled);
188 free (demangled);
191 break;
193 case 'B':
194 /* filename from a bfd */
196 bfd *abfd = va_arg (arg, bfd *);
197 if (abfd->my_archive)
198 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
199 abfd->filename);
200 else
201 fprintf (fp, "%s", abfd->filename);
203 break;
205 case 'F':
206 /* Error is fatal. */
207 fatal = true;
208 break;
210 case 'P':
211 /* Print program name. */
212 fprintf (fp, "%s", program_name);
213 break;
215 case 'E':
216 /* current bfd error or errno */
217 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
218 break;
220 case 'I':
221 /* filename from a lang_input_statement_type */
223 lang_input_statement_type *i;
225 i = va_arg (arg, lang_input_statement_type *);
226 if (bfd_my_archive (i->the_bfd) != NULL)
227 fprintf (fp, "(%s)",
228 bfd_get_filename (bfd_my_archive (i->the_bfd)));
229 fprintf (fp, "%s", i->local_sym_name);
230 if (bfd_my_archive (i->the_bfd) == NULL
231 && strcmp (i->local_sym_name, i->filename) != 0)
232 fprintf (fp, " (%s)", i->filename);
234 break;
236 case 'S':
237 /* Print script file and linenumber. */
238 if (parsing_defsym)
239 fprintf (fp, "--defsym %s", lex_string);
240 else if (ldfile_input_filename != NULL)
241 fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
242 else
243 fprintf (fp, _("built in linker script:%u"), lineno);
244 break;
246 case 'R':
247 /* Print all that's interesting about a relent. */
249 arelent *relent = va_arg (arg, arelent *);
251 lfinfo (fp, "%s+0x%v (type %s)",
252 (*(relent->sym_ptr_ptr))->name,
253 relent->addend,
254 relent->howto->name);
256 break;
258 case 'C':
259 case 'D':
260 case 'G':
261 /* Clever filename:linenumber with function name if possible,
262 or section name as a last resort. The arguments are a BFD,
263 a section, and an offset. */
265 static bfd *last_bfd;
266 static char *last_file = NULL;
267 static char *last_function = NULL;
268 bfd *abfd;
269 asection *section;
270 bfd_vma offset;
271 lang_input_statement_type *entry;
272 asymbol **asymbols;
273 const char *filename;
274 const char *functionname;
275 unsigned int linenumber;
276 boolean discard_last;
278 abfd = va_arg (arg, bfd *);
279 section = va_arg (arg, asection *);
280 offset = va_arg (arg, bfd_vma);
282 entry = (lang_input_statement_type *) abfd->usrdata;
283 if (entry != (lang_input_statement_type *) NULL
284 && entry->asymbols != (asymbol **) NULL)
285 asymbols = entry->asymbols;
286 else
288 long symsize;
289 long symbol_count;
291 symsize = bfd_get_symtab_upper_bound (abfd);
292 if (symsize < 0)
293 einfo (_("%B%F: could not read symbols\n"), abfd);
294 asymbols = (asymbol **) xmalloc (symsize);
295 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
296 if (symbol_count < 0)
297 einfo (_("%B%F: could not read symbols\n"), abfd);
298 if (entry != (lang_input_statement_type *) NULL)
300 entry->asymbols = asymbols;
301 entry->symbol_count = symbol_count;
305 discard_last = true;
306 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
307 &filename, &functionname,
308 &linenumber))
310 if (functionname != NULL && fmt[-1] == 'G')
312 lfinfo (fp, "%B:", abfd);
313 if (filename != NULL
314 && strcmp (filename, bfd_get_filename (abfd)) != 0)
315 fprintf (fp, "%s:", filename);
316 lfinfo (fp, "%T", functionname);
318 else if (functionname != NULL && fmt[-1] == 'C')
320 if (filename == (char *) NULL)
321 filename = abfd->filename;
323 if (last_bfd == NULL
324 || last_file == NULL
325 || last_function == NULL
326 || last_bfd != abfd
327 || strcmp (last_file, filename) != 0
328 || strcmp (last_function, functionname) != 0)
330 /* We use abfd->filename in this initial line,
331 in case filename is a .h file or something
332 similarly unhelpful. */
333 lfinfo (fp, _("%B: In function `%T':\n"),
334 abfd, functionname);
336 last_bfd = abfd;
337 if (last_file != NULL)
338 free (last_file);
339 last_file = xstrdup (filename);
340 if (last_function != NULL)
341 free (last_function);
342 last_function = xstrdup (functionname);
344 discard_last = false;
345 if (linenumber != 0)
346 fprintf (fp, "%s:%u", filename, linenumber);
347 else
348 lfinfo (fp, "%s(%s+0x%v)", filename, section->name,
349 offset);
351 else if (filename == NULL
352 || strcmp (filename, abfd->filename) == 0)
354 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name,
355 offset);
356 if (linenumber != 0)
357 lfinfo (fp, ":%u", linenumber);
359 else if (linenumber != 0)
360 lfinfo (fp, "%B:%s:%u", abfd, filename, linenumber);
361 else
362 lfinfo (fp, "%B(%s+0x%v):%s", abfd, section->name,
363 offset, filename);
365 else
366 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
368 if (discard_last)
370 last_bfd = NULL;
371 if (last_file != NULL)
373 free (last_file);
374 last_file = NULL;
376 if (last_function != NULL)
378 free (last_function);
379 last_function = NULL;
383 break;
385 case 's':
386 /* arbitrary string, like printf */
387 fprintf (fp, "%s", va_arg (arg, char *));
388 break;
390 case 'd':
391 /* integer, like printf */
392 fprintf (fp, "%d", va_arg (arg, int));
393 break;
395 case 'u':
396 /* unsigned integer, like printf */
397 fprintf (fp, "%u", va_arg (arg, unsigned int));
398 break;
403 if (config.fatal_warnings)
404 config.make_executable = false;
406 if (fatal == true)
407 xexit (1);
410 /* Format info message and print on stdout. */
412 /* (You would think this should be called just "info", but then you
413 would hosed by LynxOS, which defines that name in its libc.) */
415 void
416 info_msg VPARAMS ((const char *fmt, ...))
418 VA_OPEN (arg, fmt);
419 VA_FIXEDARG (arg, const char *, fmt);
421 vfinfo (stdout, fmt, arg);
422 VA_CLOSE (arg);
425 /* ('e' for error.) Format info message and print on stderr. */
427 void
428 einfo VPARAMS ((const char *fmt, ...))
430 VA_OPEN (arg, fmt);
431 VA_FIXEDARG (arg, const char *, fmt);
433 vfinfo (stderr, fmt, arg);
434 VA_CLOSE (arg);
437 void
438 info_assert (file, line)
439 const char *file;
440 unsigned int line;
442 einfo (_("%F%P: internal error %s %d\n"), file, line);
445 /* ('m' for map) Format info message and print on map. */
447 void
448 minfo VPARAMS ((const char *fmt, ...))
450 VA_OPEN (arg, fmt);
451 VA_FIXEDARG (arg, const char *, fmt);
453 vfinfo (config.map_file, fmt, arg);
454 VA_CLOSE (arg);
457 void
458 lfinfo VPARAMS ((FILE *file, const char *fmt, ...))
460 VA_OPEN (arg, fmt);
461 VA_FIXEDARG (arg, FILE *, file);
462 VA_FIXEDARG (arg, const char *, fmt);
464 vfinfo (file, fmt, arg);
465 VA_CLOSE (arg);
468 /* Functions to print the link map. */
470 void
471 print_space ()
473 fprintf (config.map_file, " ");
476 void
477 print_nl ()
479 fprintf (config.map_file, "\n");
482 /* A more or less friendly abort message. In ld.h abort is defined to
483 call this function. */
485 void
486 ld_abort (file, line, fn)
487 const char *file;
488 int line;
489 const char *fn;
491 if (fn != NULL)
492 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
493 file, line, fn);
494 else
495 einfo (_("%P: internal error: aborting at %s line %d\n"),
496 file, line);
497 einfo (_("%P%F: please report this bug\n"));
498 xexit (1);