* gdbtypes.c (check_stub_method): Make sure we get back a function
[deliverable/binutils-gdb.git] / ld / ldmisc.c
1 /* ldmisc.c
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3
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
20 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include <demangle.h>
25 /* this collection of routines wants to use the Unix style varargs
26 use special abbreviated portion of varargs.h */
27 #ifdef WINDOWS_NT
28 /* Since macro __STDC__ is defined, the compiler will raise and error if
29 VARARGS.H from mstools\h is included. Since we only need a portion of
30 this header file, it has been incorporated into local header file
31 xvarargs.h */
32 #include "xvarargs.h"
33 #else
34 #include <varargs.h>
35 #endif
36
37 #include "ld.h"
38 #include "ldmisc.h"
39 #include "ldexp.h"
40 #include "ldlang.h"
41 #include "ldgram.h"
42 #include "ldlex.h"
43 #include "ldmain.h"
44 #include "ldfile.h"
45
46
47 /* VARARGS*/
48 static void finfo ();
49 static const char *demangle PARAMS ((const char *string,
50 int remove_underscore));
51
52 /*
53 %% literal %
54 %F error is fatal
55 %P print program name
56 %S print script file and linenumber
57 %E current bfd error or errno
58 %I filename from a lang_input_statement_type
59 %B filename from a bfd
60 %T symbol name
61 %X no object output, fail return
62 %V hex bfd_vma
63 %v hex bfd_vma, no leading zeros
64 %C clever filename:linenumber with function
65 %D like %C, but no function name
66 %R info about a relent
67 %s arbitrary string, like printf
68 %d integer, like printf
69 %u integer, like printf
70 */
71
72 static const char *
73 demangle (string, remove_underscore)
74 const char *string;
75 int remove_underscore;
76 {
77 const char *res;
78 if (remove_underscore && output_bfd)
79 {
80 if (bfd_get_symbol_leading_char (output_bfd) == string[0])
81 string++;
82 }
83 /* Note that there's a memory leak here, we keep buying memory
84 for demangled names, and never free. But if you have so many
85 errors that you run out of VM with the error messages, then
86 there's something up */
87 res = cplus_demangle (string, DMGL_ANSI|DMGL_PARAMS);
88 return res ? res : string;
89 }
90
91 static void
92 vfinfo(fp, fmt, arg)
93 FILE *fp;
94 char *fmt;
95 va_list arg;
96 {
97 boolean fatal = false;
98
99 while (*fmt)
100 {
101 while (*fmt != '%' && *fmt != '\0')
102 {
103 putc(*fmt, fp);
104 fmt++;
105 }
106
107 if (*fmt == '%')
108 {
109 fmt ++;
110 switch (*fmt++)
111 {
112 default:
113 fprintf(fp,"%%%c", fmt[-1]);
114 break;
115
116 case '%':
117 /* literal % */
118 putc('%', fp);
119 break;
120
121 case 'X':
122 /* no object output, fail return */
123 config.make_executable = false;
124 break;
125
126 case 'V':
127 /* hex bfd_vma */
128 {
129 bfd_vma value = va_arg(arg, bfd_vma);
130 fprintf_vma(fp, value);
131 }
132 break;
133
134 case 'v':
135 /* hex bfd_vma, no leading zeros */
136 {
137 char buf[100];
138 char *p = buf;
139 bfd_vma value = va_arg (arg, bfd_vma);
140 sprintf_vma (p, value);
141 while (*p == '0')
142 p++;
143 if (!*p)
144 p--;
145 fputs (p, fp);
146 }
147 break;
148
149 case 'T':
150 /* Symbol name. */
151 {
152 const char *name = va_arg (arg, const char *);
153
154 if (name != (const char *) NULL)
155 fprintf (fp, "%s", demangle (name, 1));
156 else
157 fprintf (fp, "no symbol");
158 }
159 break;
160
161 case 'B':
162 /* filename from a bfd */
163 {
164 bfd *abfd = va_arg(arg, bfd *);
165 if (abfd->my_archive) {
166 fprintf(fp,"%s(%s)", abfd->my_archive->filename,
167 abfd->filename);
168 }
169 else {
170 fprintf(fp,"%s", abfd->filename);
171 }
172 }
173 break;
174
175 case 'F':
176 /* error is fatal */
177 fatal = true;
178 break;
179
180 case 'P':
181 /* print program name */
182 fprintf(fp,"%s", program_name);
183 break;
184
185 case 'E':
186 /* current bfd error or errno */
187 fprintf(fp, bfd_errmsg(bfd_get_error ()));
188 break;
189
190 case 'I':
191 /* filename from a lang_input_statement_type */
192 {
193 lang_input_statement_type *i =
194 va_arg(arg,lang_input_statement_type *);
195
196 if (i->the_bfd->my_archive)
197 fprintf(fp, "(%s)", i->the_bfd->my_archive->filename);
198 fprintf(fp,"%s", i->local_sym_name);
199 }
200 break;
201
202 case 'S':
203 /* print script file and linenumber */
204 if (parsing_defsym)
205 fprintf (fp, "--defsym %s", lex_string);
206 else if (ldfile_input_filename != NULL)
207 fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
208 else
209 fprintf (fp, "built in linker script:%u", lineno);
210 break;
211
212 case 'R':
213 /* Print all that's interesting about a relent */
214 {
215 arelent *relent = va_arg(arg, arelent *);
216
217 finfo (fp, "%s+0x%v (type %s)",
218 (*(relent->sym_ptr_ptr))->name,
219 relent->addend,
220 relent->howto->name);
221 }
222 break;
223
224 case 'C':
225 case 'D':
226 /* Clever filename:linenumber with function name if possible,
227 or section name as a last resort. The arguments are a BFD,
228 a section, and an offset. */
229 {
230 static bfd *last_bfd;
231 static char *last_file = NULL;
232 static char *last_function = NULL;
233 bfd *abfd;
234 asection *section;
235 bfd_vma offset;
236 lang_input_statement_type *entry;
237 asymbol **asymbols;
238 const char *filename;
239 const char *functionname;
240 unsigned int linenumber;
241 boolean discard_last;
242
243 abfd = va_arg (arg, bfd *);
244 section = va_arg (arg, asection *);
245 offset = va_arg (arg, bfd_vma);
246
247 entry = (lang_input_statement_type *) abfd->usrdata;
248 if (entry != (lang_input_statement_type *) NULL
249 && entry->asymbols != (asymbol **) NULL)
250 asymbols = entry->asymbols;
251 else
252 {
253 long symsize;
254 long symbol_count;
255
256 symsize = bfd_get_symtab_upper_bound (abfd);
257 if (symsize < 0)
258 einfo ("%B%F: could not read symbols\n", abfd);
259 asymbols = (asymbol **) xmalloc (symsize);
260 symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
261 if (symbol_count < 0)
262 einfo ("%B%F: could not read symbols\n", abfd);
263 if (entry != (lang_input_statement_type *) NULL)
264 {
265 entry->asymbols = asymbols;
266 entry->symbol_count = symbol_count;
267 }
268 }
269
270 discard_last = true;
271 if (bfd_find_nearest_line (abfd, section, asymbols, offset,
272 &filename, &functionname, &linenumber))
273 {
274 if (functionname != NULL && fmt[-1] == 'C')
275 {
276 if (filename == (char *) NULL)
277 filename = abfd->filename;
278
279 if (last_bfd == NULL
280 || last_file == NULL
281 || last_function == NULL
282 || last_bfd != abfd
283 || strcmp (last_file, filename) != 0
284 || strcmp (last_function, functionname) != 0)
285 {
286 /* We use abfd->filename in this initial line,
287 in case filename is a .h file or something
288 similarly unhelpful. */
289 finfo (fp, "%B: In function `%s':\n",
290 abfd, demangle (functionname, 1));
291
292 last_bfd = abfd;
293 if (last_file != NULL)
294 free (last_file);
295 last_file = buystring (filename);
296 if (last_function != NULL)
297 free (last_function);
298 last_function = buystring (functionname);
299 }
300 discard_last = false;
301 if (linenumber != 0)
302 fprintf (fp, "%s:%u", filename, linenumber);
303 else
304 finfo (fp, "%s(%s+0x%v)", filename, section->name, offset);
305 }
306 else if (filename == NULL
307 || strcmp (filename, abfd->filename) == 0)
308 {
309 finfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
310 if (linenumber != 0)
311 finfo (fp, "%u", linenumber);
312 }
313 else if (linenumber != 0)
314 finfo (fp, "%B:%s:%u", abfd, filename, linenumber);
315 else
316 finfo (fp, "%B(%s+0x%v):%s", abfd, section->name, offset,
317 filename);
318 }
319 else
320 finfo (fp, "%B(%s+0x%v)", abfd, section->name, offset);
321
322 if (discard_last)
323 {
324 last_bfd = NULL;
325 if (last_file != NULL)
326 {
327 free (last_file);
328 last_file = NULL;
329 }
330 if (last_function != NULL)
331 {
332 free (last_function);
333 last_function = NULL;
334 }
335 }
336 }
337 break;
338
339 case 's':
340 /* arbitrary string, like printf */
341 fprintf(fp,"%s", va_arg(arg, char *));
342 break;
343
344 case 'd':
345 /* integer, like printf */
346 fprintf(fp,"%d", va_arg(arg, int));
347 break;
348
349 case 'u':
350 /* unsigned integer, like printf */
351 fprintf(fp,"%u", va_arg(arg, unsigned int));
352 break;
353 }
354 }
355 }
356
357 if (fatal == true)
358 xexit(1);
359 }
360
361 /* Format info message and print on stdout. */
362
363 /* (You would think this should be called just "info", but then you would
364 hosed by LynxOS, which defines that name in its libc.) */
365
366 void info_msg(va_alist)
367 va_dcl
368 {
369 char *fmt;
370 va_list arg;
371 va_start(arg);
372 fmt = va_arg(arg, char *);
373 vfinfo(stdout, fmt, arg);
374 va_end(arg);
375 }
376
377 /* ('e' for error.) Format info message and print on stderr. */
378
379 void einfo(va_alist)
380 va_dcl
381 {
382 char *fmt;
383 va_list arg;
384 va_start(arg);
385 fmt = va_arg(arg, char *);
386 vfinfo(stderr, fmt, arg);
387 va_end(arg);
388 }
389
390 void
391 info_assert(file, line)
392 char *file;
393 unsigned int line;
394 {
395 einfo("%F%P: internal error %s %d\n", file,line);
396 }
397
398 char *
399 buystring (x)
400 CONST char *CONST x;
401 {
402 size_t l = strlen(x)+1;
403 char *r = xmalloc(l);
404 memcpy(r, x,l);
405 return r;
406 }
407
408
409 /* ('m' for map) Format info message and print on map. */
410
411 void minfo(va_alist)
412 va_dcl
413 {
414 char *fmt;
415 va_list arg;
416 va_start(arg);
417 fmt = va_arg(arg, char *);
418 vfinfo(config.map_file, fmt, arg);
419 va_end(arg);
420 }
421
422
423 static void
424 finfo (va_alist)
425 va_dcl
426 {
427 char *fmt;
428 FILE *file;
429 va_list arg;
430 va_start (arg);
431 file = va_arg (arg, FILE *);
432 fmt = va_arg (arg, char *);
433 vfinfo (file, fmt, arg);
434 va_end (arg);
435 }
436
437
438
439 /*----------------------------------------------------------------------
440 Functions to print the link map
441 */
442
443 void
444 print_space ()
445 {
446 fprintf(config.map_file, " ");
447 }
448 void
449 print_nl ()
450 {
451 fprintf(config.map_file, "\n");
452 }
453 void
454 print_address (value)
455 bfd_vma value;
456 {
457 fprintf_vma(config.map_file, value);
458 }
This page took 0.049788 seconds and 4 git commands to generate.