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