1 /* ldmisc.c
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5    Written by Steve Chamberlain of Cygnus Support.
6 
7    This file is part of GLD, the Gnu Linker.
8 
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.
13 
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.
18 
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, 51 Franklin Street - Fifth Floor, Boston, MA
22    02110-1301, USA.  */
23 
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "sysdep.h"
27 #include "libiberty.h"
28 #include "demangle.h"
29 #include <stdarg.h>
30 #include "ld.h"
31 #include "ldmisc.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldmain.h"
37 #include "ldfile.h"
38 #include "elf-bfd.h"
39 
40 /*
41  %% literal %
42  %A section name from a section
43  %B filename from a bfd
44  %C clever filename:linenumber with function
45  %D like %C, but no function name
46  %E current bfd error or errno
47  %F error is fatal
48  %G like %D, but only function name
49  %I filename from a lang_input_statement_type
50  %P print program name
51  %R info about a relent
52  %S print script file and linenumber
53  %T symbol name
54  %V hex bfd_vma
55  %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
56  %X no object output, fail return
57  %d integer, like printf
58  %s arbitrary string, like printf
59  %u integer, like printf
60  %v hex bfd_vma, no leading zeros
61 */
62 
63 static void
vfinfo(FILE * fp,const char * fmt,va_list arg,bfd_boolean is_warning)64 vfinfo (FILE *fp, const char *fmt, va_list arg, bfd_boolean is_warning)
65 {
66   bfd_boolean fatal = FALSE;
67 
68   while (*fmt != '\0')
69     {
70       while (*fmt != '%' && *fmt != '\0')
71 	{
72 	  putc (*fmt, fp);
73 	  fmt++;
74 	}
75 
76       if (*fmt == '%')
77 	{
78 	  fmt++;
79 	  switch (*fmt++)
80 	    {
81 	    default:
82 	      fprintf (fp, "%%%c", fmt[-1]);
83 	      break;
84 
85 	    case '%':
86 	      /* literal % */
87 	      putc ('%', fp);
88 	      break;
89 
90 	    case 'X':
91 	      /* no object output, fail return */
92 	      config.make_executable = FALSE;
93 	      break;
94 
95 	    case 'V':
96 	      /* hex bfd_vma */
97 	      {
98 		bfd_vma value = va_arg (arg, bfd_vma);
99 		fprintf_vma (fp, value);
100 	      }
101 	      break;
102 
103 	    case 'v':
104 	      /* hex bfd_vma, no leading zeros */
105 	      {
106 		char buf[100];
107 		char *p = buf;
108 		bfd_vma value = va_arg (arg, bfd_vma);
109 		sprintf_vma (p, value);
110 		while (*p == '0')
111 		  p++;
112 		if (!*p)
113 		  p--;
114 		fputs (p, fp);
115 	      }
116 	      break;
117 
118 	    case 'W':
119 	      /* hex bfd_vma with 0x with no leading zeroes taking up
120 		 8 spaces.  */
121 	      {
122 		char buf[100];
123 		bfd_vma value;
124 		char *p;
125 		int len;
126 
127 		value = va_arg (arg, bfd_vma);
128 		sprintf_vma (buf, value);
129 		for (p = buf; *p == '0'; ++p)
130 		  ;
131 		if (*p == '\0')
132 		  --p;
133 		len = strlen (p);
134 		while (len < 8)
135 		  {
136 		    putc (' ', fp);
137 		    ++len;
138 		  }
139 		fprintf (fp, "0x%s", p);
140 	      }
141 	      break;
142 
143 	    case 'T':
144 	      /* Symbol name.  */
145 	      {
146 		const char *name = va_arg (arg, const char *);
147 
148 		if (name == NULL || *name == 0)
149 		  fprintf (fp, _("no symbol"));
150 		else if (! demangling)
151 		  fprintf (fp, "%s", name);
152 		else
153 		  {
154 		    char *demangled;
155 
156 		    demangled = demangle (name);
157 		    fprintf (fp, "%s", demangled);
158 		    free (demangled);
159 		  }
160 	      }
161 	      break;
162 
163 	    case 'A':
164 	      /* section name from a section */
165 	      {
166 		asection *sec = va_arg (arg, asection *);
167 		bfd *abfd = sec->owner;
168 		const char *group = NULL;
169 		struct coff_comdat_info *ci;
170 
171 		fprintf (fp, "%s", sec->name);
172 		if (abfd != NULL
173 		    && bfd_get_flavour (abfd) == bfd_target_elf_flavour
174 		    && elf_next_in_group (sec) != NULL
175 		    && (sec->flags & SEC_GROUP) == 0)
176 		  group = elf_group_name (sec);
177 		else if (abfd != NULL
178 			 && bfd_get_flavour (abfd) == bfd_target_coff_flavour
179 			 && (ci = bfd_coff_get_comdat_section (sec->owner,
180 							       sec)) != NULL)
181 		  group = ci->name;
182 		if (group != NULL)
183 		  fprintf (fp, "[%s]", group);
184 	      }
185 	      break;
186 
187 	    case 'B':
188 	      /* filename from a bfd */
189 	      {
190 		bfd *abfd = va_arg (arg, bfd *);
191 
192 		if (abfd == NULL)
193 		  fprintf (fp, "%s generated", program_name);
194 		else 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 		 The arguments are a BFD, a section, and an offset.  */
260 	      {
261 		static bfd *last_bfd;
262 		static char *last_file = NULL;
263 		static char *last_function = NULL;
264 		bfd *abfd;
265 		asection *section;
266 		bfd_vma offset;
267 		lang_input_statement_type *entry;
268 		asymbol **asymbols;
269 		const char *filename;
270 		const char *functionname;
271 		unsigned int linenumber;
272 		bfd_boolean discard_last;
273 
274 		abfd = va_arg (arg, bfd *);
275 		section = va_arg (arg, asection *);
276 		offset = va_arg (arg, bfd_vma);
277 
278 		if (abfd == NULL)
279 		  {
280 		    entry = NULL;
281 		    asymbols = NULL;
282 		  }
283 		else
284 		  {
285 		    entry = (lang_input_statement_type *) abfd->usrdata;
286 		    if (entry != (lang_input_statement_type *) NULL
287 			&& entry->asymbols != (asymbol **) NULL)
288 		      asymbols = entry->asymbols;
289 		    else
290 		      {
291 			long symsize;
292 			long sym_count;
293 
294 			symsize = bfd_get_symtab_upper_bound (abfd);
295 			if (symsize < 0)
296 			  einfo (_("%B%F: could not read symbols\n"), abfd);
297 			asymbols = xmalloc (symsize);
298 			sym_count = bfd_canonicalize_symtab (abfd, asymbols);
299 			if (sym_count < 0)
300 			  einfo (_("%B%F: could not read symbols\n"), abfd);
301 			if (entry != (lang_input_statement_type *) NULL)
302 			  {
303 			    entry->asymbols = asymbols;
304 			    entry->symbol_count = sym_count;
305 			  }
306 		      }
307 		  }
308 
309 		/* The GNU Coding Standard requires that error messages
310 		   be of the form:
311 
312 		     source-file-name:lineno: message
313 
314 		   We do not always have a line number available so if
315 		   we cannot find them we print out the section name and
316 		   offset instread.  */
317 		discard_last = TRUE;
318 		if (abfd != NULL
319 		    && bfd_find_nearest_line (abfd, section, asymbols, offset,
320 					      &filename, &functionname,
321 					      &linenumber))
322 		  {
323 		    if (functionname != NULL && fmt[-1] == 'C')
324 		      {
325 			/* Detect the case where we are printing out a
326 			   message for the same function as the last
327 			   call to vinfo ("%C").  In this situation do
328 			   not print out the ABFD filename or the
329 			   function name again.  Note - we do still
330 			   print out the source filename, as this will
331 			   allow programs that parse the linker's output
332 			   (eg emacs) to correctly locate multiple
333 			   errors in the same source file.  */
334 			if (last_bfd == NULL
335 			    || last_file == NULL
336 			    || last_function == NULL
337 			    || last_bfd != abfd
338 			    || (filename != NULL
339 				&& strcmp (last_file, filename) != 0)
340 			    || strcmp (last_function, functionname) != 0)
341 			  {
342 			    lfinfo (fp, _("%B: In function `%T':\n"),
343 				    abfd, functionname);
344 
345 			    last_bfd = abfd;
346 			    if (last_file != NULL)
347 			      free (last_file);
348 			    last_file = NULL;
349 			    if (filename)
350 			      last_file = xstrdup (filename);
351 			    if (last_function != NULL)
352 			      free (last_function);
353 			    last_function = xstrdup (functionname);
354 			  }
355 			discard_last = FALSE;
356 		      }
357 		    else
358 		      lfinfo (fp, "%B:", abfd);
359 
360 		    if (filename != NULL)
361 		      fprintf (fp, "%s:", filename);
362 
363 		    if (functionname != NULL && fmt[-1] == 'G')
364 		      lfinfo (fp, "%T", functionname);
365 		    else if (filename != NULL)
366 		      {
367 			if (linenumber != 0)
368 			  fprintf (fp, "%u", linenumber);
369 			else
370 			  lfinfo (fp, "(%A+0x%v)", section, offset);
371 		      }
372 		  }
373 		else
374 		  lfinfo (fp, "%B:(%A+0x%v)", abfd, section, offset);
375 
376 		if (asymbols != NULL && entry == NULL)
377 		  free (asymbols);
378 
379 		if (discard_last)
380 		  {
381 		    last_bfd = NULL;
382 		    if (last_file != NULL)
383 		      {
384 			free (last_file);
385 			last_file = NULL;
386 		      }
387 		    if (last_function != NULL)
388 		      {
389 			free (last_function);
390 			last_function = NULL;
391 		      }
392 		  }
393 	      }
394 	      break;
395 
396 	    case 's':
397 	      /* arbitrary string, like printf */
398 	      fprintf (fp, "%s", va_arg (arg, char *));
399 	      break;
400 
401 	    case 'd':
402 	      /* integer, like printf */
403 	      fprintf (fp, "%d", va_arg (arg, int));
404 	      break;
405 
406 	    case 'u':
407 	      /* unsigned integer, like printf */
408 	      fprintf (fp, "%u", va_arg (arg, unsigned int));
409 	      break;
410 	    }
411 	}
412     }
413 
414   if (is_warning && config.fatal_warnings)
415     config.make_executable = FALSE;
416 
417   if (fatal)
418     xexit (1);
419 }
420 
421 /* Wrapper around cplus_demangle.  Strips leading underscores and
422    other such chars that would otherwise confuse the demangler.  */
423 
424 char *
demangle(const char * name)425 demangle (const char *name)
426 {
427   char *res;
428   const char *p;
429 
430   if (output_bfd != NULL
431       && bfd_get_symbol_leading_char (output_bfd) == name[0])
432     ++name;
433 
434   /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
435      or the MS PE format.  These formats have a number of leading '.'s
436      on at least some symbols, so we remove all dots to avoid
437      confusing the demangler.  */
438   p = name;
439   while (*p == '.')
440     ++p;
441 
442   res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
443   if (res)
444     {
445       size_t dots = p - name;
446 
447       /* Now put back any stripped dots.  */
448       if (dots != 0)
449 	{
450 	  size_t len = strlen (res) + 1;
451 	  char *add_dots = xmalloc (len + dots);
452 
453 	  memcpy (add_dots, name, dots);
454 	  memcpy (add_dots + dots, res, len);
455 	  free (res);
456 	  res = add_dots;
457 	}
458       return res;
459     }
460   return xstrdup (name);
461 }
462 
463 /* Format info message and print on stdout.  */
464 
465 /* (You would think this should be called just "info", but then you
466    would be hosed by LynxOS, which defines that name in its libc.)  */
467 
468 void
info_msg(const char * fmt,...)469 info_msg (const char *fmt, ...)
470 {
471   va_list arg;
472 
473   va_start (arg, fmt);
474   vfinfo (stdout, fmt, arg, FALSE);
475   va_end (arg);
476 }
477 
478 /* ('e' for error.) Format info message and print on stderr.  */
479 
480 void
einfo(const char * fmt,...)481 einfo (const char *fmt, ...)
482 {
483   va_list arg;
484 
485   va_start (arg, fmt);
486   vfinfo (stderr, fmt, arg, TRUE);
487   va_end (arg);
488 }
489 
490 void
info_assert(const char * file,unsigned int line)491 info_assert (const char *file, unsigned int line)
492 {
493   einfo (_("%F%P: internal error %s %d\n"), file, line);
494 }
495 
496 /* ('m' for map) Format info message and print on map.  */
497 
498 void
minfo(const char * fmt,...)499 minfo (const char *fmt, ...)
500 {
501   va_list arg;
502 
503   va_start (arg, fmt);
504   vfinfo (config.map_file, fmt, arg, FALSE);
505   va_end (arg);
506 }
507 
508 void
lfinfo(FILE * file,const char * fmt,...)509 lfinfo (FILE *file, const char *fmt, ...)
510 {
511   va_list arg;
512 
513   va_start (arg, fmt);
514   vfinfo (file, fmt, arg, FALSE);
515   va_end (arg);
516 }
517 
518 /* Functions to print the link map.  */
519 
520 void
print_space(void)521 print_space (void)
522 {
523   fprintf (config.map_file, " ");
524 }
525 
526 void
print_nl(void)527 print_nl (void)
528 {
529   fprintf (config.map_file, "\n");
530 }
531 
532 /* A more or less friendly abort message.  In ld.h abort is defined to
533    call this function.  */
534 
535 void
ld_abort(const char * file,int line,const char * fn)536 ld_abort (const char *file, int line, const char *fn)
537 {
538   if (fn != NULL)
539     einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
540 	   file, line, fn);
541   else
542     einfo (_("%P: internal error: aborting at %s line %d\n"),
543 	   file, line);
544   einfo (_("%P%F: please report this bug\n"));
545   xexit (1);
546 }
547