* rcparse.y (res_text_field): New res_id variable.
[deliverable/binutils-gdb.git] / ld / ldmisc.c
CommitLineData
252b5132 1/* ldmisc.c
6d39955e 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
5ed6aba4 3 2000, 2002, 2003
252b5132
RH
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support.
6
5ed6aba4 7 This file is part of GLD, the Gnu Linker.
252b5132 8
5ed6aba4
NC
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.
252b5132 13
5ed6aba4
NC
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.
252b5132 18
5ed6aba4
NC
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. */
252b5132
RH
23
24#include "bfd.h"
6f6f27f8 25#include "bfdlink.h"
252b5132
RH
26#include "sysdep.h"
27#include "libiberty.h"
28#include "demangle.h"
29
30#ifdef ANSI_PROTOTYPES
31#include <stdarg.h>
252b5132
RH
32#else
33#include <varargs.h>
252b5132
RH
34#endif
35
36#include "ld.h"
37#include "ldmisc.h"
38#include "ldexp.h"
39#include "ldlang.h"
df2a7313 40#include <ldgram.h>
252b5132
RH
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
252b5132
RH
69static void
70vfinfo (fp, fmt, arg)
71 FILE *fp;
72 const char *fmt;
73 va_list arg;
74{
b34976b6 75 bfd_boolean fatal = FALSE;
252b5132
RH
76
77 while (*fmt != '\0')
78 {
6d5e62f8 79 while (*fmt != '%' && *fmt != '\0')
252b5132
RH
80 {
81 putc (*fmt, fp);
82 fmt++;
83 }
84
6d5e62f8 85 if (*fmt == '%')
252b5132 86 {
6d5e62f8
KH
87 fmt++;
88 switch (*fmt++)
252b5132
RH
89 {
90 default:
6d5e62f8 91 fprintf (fp, "%%%c", fmt[-1]);
252b5132
RH
92 break;
93
94 case '%':
95 /* literal % */
96 putc ('%', fp);
97 break;
98
99 case 'X':
100 /* no object output, fail return */
b34976b6 101 config.make_executable = FALSE;
252b5132
RH
102 break;
103
104 case 'V':
105 /* hex bfd_vma */
106 {
107 bfd_vma value = va_arg (arg, bfd_vma);
108 fprintf_vma (fp, value);
109 }
110 break;
111
112 case 'v':
113 /* hex bfd_vma, no leading zeros */
114 {
115 char buf[100];
116 char *p = buf;
117 bfd_vma value = va_arg (arg, bfd_vma);
118 sprintf_vma (p, value);
119 while (*p == '0')
120 p++;
121 if (!*p)
122 p--;
123 fputs (p, fp);
124 }
125 break;
126
127 case 'W':
128 /* hex bfd_vma with 0x with no leading zeroes taking up
129 8 spaces. */
130 {
131 char buf[100];
132 bfd_vma value;
133 char *p;
134 int len;
135
136 value = va_arg (arg, bfd_vma);
137 sprintf_vma (buf, value);
138 for (p = buf; *p == '0'; ++p)
139 ;
140 if (*p == '\0')
141 --p;
142 len = strlen (p);
143 while (len < 8)
144 {
145 putc (' ', fp);
146 ++len;
147 }
148 fprintf (fp, "0x%s", p);
149 }
150 break;
151
152 case 'T':
153 /* Symbol name. */
154 {
155 const char *name = va_arg (arg, const char *);
156
157 if (name == (const char *) NULL || *name == 0)
158 fprintf (fp, _("no symbol"));
159 else if (! demangling)
160 fprintf (fp, "%s", name);
161 else
162 {
163 char *demangled;
164
165 demangled = demangle (name);
166 fprintf (fp, "%s", demangled);
167 free (demangled);
168 }
169 }
170 break;
171
172 case 'B':
173 /* filename from a bfd */
6d5e62f8 174 {
252b5132
RH
175 bfd *abfd = va_arg (arg, bfd *);
176 if (abfd->my_archive)
177 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
178 abfd->filename);
179 else
180 fprintf (fp, "%s", abfd->filename);
181 }
182 break;
183
184 case 'F':
6d5e62f8 185 /* Error is fatal. */
b34976b6 186 fatal = TRUE;
252b5132
RH
187 break;
188
189 case 'P':
6d5e62f8 190 /* Print program name. */
252b5132
RH
191 fprintf (fp, "%s", program_name);
192 break;
193
194 case 'E':
195 /* current bfd error or errno */
305c7206 196 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
252b5132
RH
197 break;
198
199 case 'I':
200 /* filename from a lang_input_statement_type */
201 {
202 lang_input_statement_type *i;
203
204 i = va_arg (arg, lang_input_statement_type *);
205 if (bfd_my_archive (i->the_bfd) != NULL)
206 fprintf (fp, "(%s)",
207 bfd_get_filename (bfd_my_archive (i->the_bfd)));
208 fprintf (fp, "%s", i->local_sym_name);
209 if (bfd_my_archive (i->the_bfd) == NULL
210 && strcmp (i->local_sym_name, i->filename) != 0)
211 fprintf (fp, " (%s)", i->filename);
212 }
213 break;
214
215 case 'S':
6d5e62f8 216 /* Print script file and linenumber. */
252b5132
RH
217 if (parsing_defsym)
218 fprintf (fp, "--defsym %s", lex_string);
219 else if (ldfile_input_filename != NULL)
220 fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
221 else
222 fprintf (fp, _("built in linker script:%u"), lineno);
223 break;
224
225 case 'R':
6d5e62f8 226 /* Print all that's interesting about a relent. */
252b5132
RH
227 {
228 arelent *relent = va_arg (arg, arelent *);
6d5e62f8 229
252b5132
RH
230 lfinfo (fp, "%s+0x%v (type %s)",
231 (*(relent->sym_ptr_ptr))->name,
232 relent->addend,
233 relent->howto->name);
234 }
235 break;
6d5e62f8 236
252b5132
RH
237 case 'C':
238 case 'D':
239 case 'G':
5cfb2bb2
AM
240 /* Clever filename:linenumber with function name if possible.
241 The arguments are a BFD, a section, and an offset. */
252b5132
RH
242 {
243 static bfd *last_bfd;
244 static char *last_file = NULL;
245 static char *last_function = NULL;
246 bfd *abfd;
247 asection *section;
248 bfd_vma offset;
249 lang_input_statement_type *entry;
250 asymbol **asymbols;
251 const char *filename;
252 const char *functionname;
253 unsigned int linenumber;
b34976b6 254 bfd_boolean discard_last;
252b5132
RH
255
256 abfd = va_arg (arg, bfd *);
257 section = va_arg (arg, asection *);
258 offset = va_arg (arg, bfd_vma);
259
260 entry = (lang_input_statement_type *) abfd->usrdata;
261 if (entry != (lang_input_statement_type *) NULL
262 && entry->asymbols != (asymbol **) NULL)
263 asymbols = entry->asymbols;
264 else
265 {
266 long symsize;
267 long symbol_count;
268
269 symsize = bfd_get_symtab_upper_bound (abfd);
270 if (symsize < 0)
271 einfo (_("%B%F: could not read symbols\n"), abfd);
272 asymbols = (asymbol **) xmalloc (symsize);
273 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
274 if (symbol_count < 0)
275 einfo (_("%B%F: could not read symbols\n"), abfd);
276 if (entry != (lang_input_statement_type *) NULL)
277 {
278 entry->asymbols = asymbols;
279 entry->symbol_count = symbol_count;
280 }
281 }
282
5cfb2bb2
AM
283 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
284
b34976b6 285 discard_last = TRUE;
252b5132
RH
286 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
287 &filename, &functionname,
288 &linenumber))
289 {
b34976b6 290 bfd_boolean need_colon = TRUE;
252b5132 291
5cfb2bb2
AM
292 if (functionname != NULL && fmt[-1] == 'C')
293 {
252b5132
RH
294 if (last_bfd == NULL
295 || last_file == NULL
296 || last_function == NULL
297 || last_bfd != abfd
5cfb2bb2
AM
298 || (filename != NULL
299 && strcmp (last_file, filename) != 0)
252b5132
RH
300 || strcmp (last_function, functionname) != 0)
301 {
5cfb2bb2
AM
302 lfinfo (fp, _(": In function `%T':\n"),
303 functionname);
b34976b6 304 need_colon = FALSE;
252b5132
RH
305
306 last_bfd = abfd;
307 if (last_file != NULL)
308 free (last_file);
5cfb2bb2
AM
309 last_file = NULL;
310 if (filename)
311 last_file = xstrdup (filename);
252b5132
RH
312 if (last_function != NULL)
313 free (last_function);
d1b2b2dc 314 last_function = xstrdup (functionname);
252b5132 315 }
b34976b6 316 discard_last = FALSE;
252b5132 317 }
5cfb2bb2
AM
318
319 if (filename != NULL)
252b5132 320 {
5cfb2bb2
AM
321 if (need_colon)
322 putc (':', fp);
323 fputs (filename, fp);
252b5132 324 }
5cfb2bb2
AM
325
326 if (functionname != NULL && fmt[-1] == 'G')
327 lfinfo (fp, ":%T", functionname);
328 else if (filename != NULL && linenumber != 0)
329 fprintf (fp, ":%u", linenumber);
252b5132 330 }
252b5132 331
5ed6aba4
NC
332 if (asymbols != NULL && entry == NULL)
333 free (asymbols);
334
252b5132
RH
335 if (discard_last)
336 {
337 last_bfd = NULL;
338 if (last_file != NULL)
339 {
340 free (last_file);
341 last_file = NULL;
342 }
343 if (last_function != NULL)
344 {
345 free (last_function);
346 last_function = NULL;
347 }
348 }
349 }
350 break;
6d5e62f8 351
252b5132
RH
352 case 's':
353 /* arbitrary string, like printf */
354 fprintf (fp, "%s", va_arg (arg, char *));
355 break;
356
357 case 'd':
358 /* integer, like printf */
359 fprintf (fp, "%d", va_arg (arg, int));
360 break;
361
362 case 'u':
363 /* unsigned integer, like printf */
364 fprintf (fp, "%u", va_arg (arg, unsigned int));
365 break;
366 }
367 }
368 }
369
7ce691ae 370 if (config.fatal_warnings)
b34976b6 371 config.make_executable = FALSE;
7ce691ae 372
b34976b6 373 if (fatal)
6d5e62f8 374 xexit (1);
252b5132
RH
375}
376
b7b482a3
AM
377/* Wrapper around cplus_demangle. Strips leading underscores and
378 other such chars that would otherwise confuse the demangler. */
379
380char *
381demangle (name)
382 const char *name;
383{
384 char *res;
385 const char *p;
386
387 if (output_bfd != NULL
388 && bfd_get_symbol_leading_char (output_bfd) == name[0])
389 ++name;
390
391 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
392 or the MS PE format. These formats have a number of leading '.'s
393 on at least some symbols, so we remove all dots to avoid
394 confusing the demangler. */
395 p = name;
396 while (*p == '.')
397 ++p;
398
399 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
400 if (res)
401 {
402 size_t dots = p - name;
403
404 /* Now put back any stripped dots. */
405 if (dots != 0)
406 {
407 size_t len = strlen (res) + 1;
408 char *add_dots = xmalloc (len + dots);
409
410 memcpy (add_dots, name, dots);
411 memcpy (add_dots + dots, res, len);
412 free (res);
413 res = add_dots;
414 }
415 return res;
416 }
417 return xstrdup (name);
418}
419
6d5e62f8 420/* Format info message and print on stdout. */
252b5132
RH
421
422/* (You would think this should be called just "info", but then you
423 would hosed by LynxOS, which defines that name in its libc.) */
424
425void
d5e0ebeb 426info_msg VPARAMS ((const char *fmt, ...))
252b5132 427{
d5e0ebeb
AM
428 VA_OPEN (arg, fmt);
429 VA_FIXEDARG (arg, const char *, fmt);
252b5132
RH
430
431 vfinfo (stdout, fmt, arg);
d5e0ebeb 432 VA_CLOSE (arg);
252b5132
RH
433}
434
6d5e62f8 435/* ('e' for error.) Format info message and print on stderr. */
252b5132
RH
436
437void
d5e0ebeb 438einfo VPARAMS ((const char *fmt, ...))
252b5132 439{
d5e0ebeb
AM
440 VA_OPEN (arg, fmt);
441 VA_FIXEDARG (arg, const char *, fmt);
252b5132
RH
442
443 vfinfo (stderr, fmt, arg);
d5e0ebeb 444 VA_CLOSE (arg);
252b5132
RH
445}
446
6d5e62f8 447void
252b5132
RH
448info_assert (file, line)
449 const char *file;
450 unsigned int line;
451{
452 einfo (_("%F%P: internal error %s %d\n"), file, line);
453}
454
6d5e62f8 455/* ('m' for map) Format info message and print on map. */
252b5132
RH
456
457void
d5e0ebeb 458minfo VPARAMS ((const char *fmt, ...))
252b5132 459{
d5e0ebeb
AM
460 VA_OPEN (arg, fmt);
461 VA_FIXEDARG (arg, const char *, fmt);
252b5132
RH
462
463 vfinfo (config.map_file, fmt, arg);
d5e0ebeb 464 VA_CLOSE (arg);
252b5132
RH
465}
466
467void
d5e0ebeb 468lfinfo VPARAMS ((FILE *file, const char *fmt, ...))
252b5132 469{
d5e0ebeb
AM
470 VA_OPEN (arg, fmt);
471 VA_FIXEDARG (arg, FILE *, file);
472 VA_FIXEDARG (arg, const char *, fmt);
252b5132
RH
473
474 vfinfo (file, fmt, arg);
d5e0ebeb 475 VA_CLOSE (arg);
252b5132
RH
476}
477\f
478/* Functions to print the link map. */
479
6d5e62f8 480void
252b5132
RH
481print_space ()
482{
483 fprintf (config.map_file, " ");
484}
485
6d5e62f8 486void
252b5132
RH
487print_nl ()
488{
489 fprintf (config.map_file, "\n");
490}
45455cdd
ILT
491
492/* A more or less friendly abort message. In ld.h abort is defined to
493 call this function. */
494
495void
496ld_abort (file, line, fn)
497 const char *file;
498 int line;
499 const char *fn;
500{
501 if (fn != NULL)
502 einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
503 file, line, fn);
504 else
505 einfo (_("%P: internal error: aborting at %s line %d\n"),
506 file, line);
507 einfo (_("%P%F: please report this bug\n"));
508 xexit (1);
509}
0f0569c4
L
510
511bfd_boolean
6f6f27f8 512error_handler VPARAMS ((int id, const char *fmt, ...))
0f0569c4
L
513{
514 VA_OPEN (arg, fmt);
515 VA_FIXEDARG (arg, const char *, fmt);
516
6f6f27f8
L
517 va_start (arg, fmt);
518
519 switch (id)
520 {
521 default:
522 break;
523
524 /* We can be called with
525
526 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 0);
527
528 to make this error non-fatal and
529
530 error_handler (-LD_DEFINITION_IN_DISCARDED_SECTION, "", 1);
531
532 to make this error fatal. */
533 case -LD_DEFINITION_IN_DISCARDED_SECTION:
534 case LD_DEFINITION_IN_DISCARDED_SECTION:
535 {
536 static struct bfd_hash_table *hash;
537 static int fatal = 1;
538 const char *name;
539
540 if (id == -LD_DEFINITION_IN_DISCARDED_SECTION)
541 {
542 fatal = va_arg (arg, int);
543 goto out;
544 }
545
546 name = va_arg (arg, const char *);
547 /* Only warn once about a particular undefined symbol. */
548 if (hash == NULL)
549 {
550 hash = ((struct bfd_hash_table *)
551 xmalloc (sizeof (struct bfd_hash_table)));
552 if (! bfd_hash_table_init (hash, bfd_hash_newfunc))
553 einfo (_("%F%P: bfd_hash_table_init failed: %E\n"));
554 }
555
556 if (bfd_hash_lookup (hash, name, FALSE, FALSE) != NULL)
557 goto out;
558
559 if (bfd_hash_lookup (hash, name, TRUE, TRUE) == NULL)
560 einfo (_("%F%P: bfd_hash_lookup failed: %E\n"));
561
562 if (fatal)
563 config.make_executable = FALSE;
564 }
565 break;
566 }
0f0569c4 567 vfinfo (stderr, fmt, arg);
6f6f27f8 568out:
0f0569c4
L
569 VA_CLOSE (arg);
570 return TRUE;
571}
This page took 0.200998 seconds and 4 git commands to generate.