ldmisc.c revision 104834
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. 6 7This file is part of GLD, the Gnu Linker. 8 9GLD is free software; you can redistribute it and/or modify 10it under the terms of the GNU General Public License as published by 11the Free Software Foundation; either version 2, or (at your option) 12any later version. 13 14GLD is distributed in the hope that it will be useful, 15but WITHOUT ANY WARRANTY; without even the implied warranty of 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17GNU General Public License for more details. 18 19You should have received a copy of the GNU General Public License 20along with GLD; see the file COPYING. If not, write to the Free 21Software Foundation, 59 Temple Place - Suite 330, Boston, MA 2202111-1307, USA. */ 23 24#include "bfd.h" 25#include "sysdep.h" 26#include "libiberty.h" 27#include "demangle.h" 28 29#ifdef ANSI_PROTOTYPES 30#include <stdarg.h> 31#else 32#include <varargs.h> 33#endif 34 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" 43 44static void vfinfo PARAMS ((FILE *, const char *, va_list)); 45 46/* 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 66*/ 67 68static void 69vfinfo (fp, fmt, arg) 70 FILE *fp; 71 const char *fmt; 72 va_list arg; 73{ 74 boolean fatal = false; 75 76 while (*fmt != '\0') 77 { 78 while (*fmt != '%' && *fmt != '\0') 79 { 80 putc (*fmt, fp); 81 fmt++; 82 } 83 84 if (*fmt == '%') 85 { 86 fmt++; 87 switch (*fmt++) 88 { 89 default: 90 fprintf (fp, "%%%c", fmt[-1]); 91 break; 92 93 case '%': 94 /* literal % */ 95 putc ('%', fp); 96 break; 97 98 case 'X': 99 /* no object output, fail return */ 100 config.make_executable = false; 101 break; 102 103 case 'V': 104 /* hex bfd_vma */ 105 { 106 bfd_vma value = va_arg (arg, bfd_vma); 107 fprintf_vma (fp, value); 108 } 109 break; 110 111 case 'v': 112 /* hex bfd_vma, no leading zeros */ 113 { 114 char buf[100]; 115 char *p = buf; 116 bfd_vma value = va_arg (arg, bfd_vma); 117 sprintf_vma (p, value); 118 while (*p == '0') 119 p++; 120 if (!*p) 121 p--; 122 fputs (p, fp); 123 } 124 break; 125 126 case 'W': 127 /* hex bfd_vma with 0x with no leading zeroes taking up 128 8 spaces. */ 129 { 130 char buf[100]; 131 bfd_vma value; 132 char *p; 133 int len; 134 135 value = va_arg (arg, bfd_vma); 136 sprintf_vma (buf, value); 137 for (p = buf; *p == '0'; ++p) 138 ; 139 if (*p == '\0') 140 --p; 141 len = strlen (p); 142 while (len < 8) 143 { 144 putc (' ', fp); 145 ++len; 146 } 147 fprintf (fp, "0x%s", p); 148 } 149 break; 150 151 case 'T': 152 /* Symbol name. */ 153 { 154 const char *name = va_arg (arg, const char *); 155 156 if (name == (const char *) NULL || *name == 0) 157 fprintf (fp, _("no symbol")); 158 else if (! demangling) 159 fprintf (fp, "%s", name); 160 else 161 { 162 char *demangled; 163 164 demangled = demangle (name); 165 fprintf (fp, "%s", demangled); 166 free (demangled); 167 } 168 } 169 break; 170 171 case 'B': 172 /* filename from a bfd */ 173 { 174 bfd *abfd = va_arg (arg, bfd *); 175 if (abfd->my_archive) 176 fprintf (fp, "%s(%s)", abfd->my_archive->filename, 177 abfd->filename); 178 else 179 fprintf (fp, "%s", abfd->filename); 180 } 181 break; 182 183 case 'F': 184 /* Error is fatal. */ 185 fatal = true; 186 break; 187 188 case 'P': 189 /* Print program name. */ 190 fprintf (fp, "%s", program_name); 191 break; 192 193 case 'E': 194 /* current bfd error or errno */ 195 fprintf (fp, "%s", bfd_errmsg (bfd_get_error ())); 196 break; 197 198 case 'I': 199 /* filename from a lang_input_statement_type */ 200 { 201 lang_input_statement_type *i; 202 203 i = va_arg (arg, lang_input_statement_type *); 204 if (bfd_my_archive (i->the_bfd) != NULL) 205 fprintf (fp, "(%s)", 206 bfd_get_filename (bfd_my_archive (i->the_bfd))); 207 fprintf (fp, "%s", i->local_sym_name); 208 if (bfd_my_archive (i->the_bfd) == NULL 209 && strcmp (i->local_sym_name, i->filename) != 0) 210 fprintf (fp, " (%s)", i->filename); 211 } 212 break; 213 214 case 'S': 215 /* Print script file and linenumber. */ 216 if (parsing_defsym) 217 fprintf (fp, "--defsym %s", lex_string); 218 else if (ldfile_input_filename != NULL) 219 fprintf (fp, "%s:%u", ldfile_input_filename, lineno); 220 else 221 fprintf (fp, _("built in linker script:%u"), lineno); 222 break; 223 224 case 'R': 225 /* Print all that's interesting about a relent. */ 226 { 227 arelent *relent = va_arg (arg, arelent *); 228 229 lfinfo (fp, "%s+0x%v (type %s)", 230 (*(relent->sym_ptr_ptr))->name, 231 relent->addend, 232 relent->howto->name); 233 } 234 break; 235 236 case 'C': 237 case 'D': 238 case 'G': 239 /* Clever filename:linenumber with function name if possible, 240 or section name as a last resort. The arguments are a BFD, 241 a section, and an offset. */ 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; 254 boolean discard_last; 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 283 discard_last = true; 284 if (bfd_find_nearest_line (abfd, section, asymbols, offset, 285 &filename, &functionname, 286 &linenumber)) 287 { 288 if (functionname != NULL && fmt[-1] == 'G') 289 { 290 lfinfo (fp, "%B:", abfd); 291 if (filename != NULL 292 && strcmp (filename, bfd_get_filename (abfd)) != 0) 293 fprintf (fp, "%s:", filename); 294 lfinfo (fp, "%T", functionname); 295 } 296 else if (functionname != NULL && fmt[-1] == 'C') 297 { 298 if (filename == (char *) NULL) 299 filename = abfd->filename; 300 301 if (last_bfd == NULL 302 || last_file == NULL 303 || last_function == NULL 304 || last_bfd != abfd 305 || strcmp (last_file, filename) != 0 306 || strcmp (last_function, functionname) != 0) 307 { 308 /* We use abfd->filename in this initial line, 309 in case filename is a .h file or something 310 similarly unhelpful. */ 311 lfinfo (fp, _("%B: In function `%T':\n"), 312 abfd, functionname); 313 314 last_bfd = abfd; 315 if (last_file != NULL) 316 free (last_file); 317 last_file = xstrdup (filename); 318 if (last_function != NULL) 319 free (last_function); 320 last_function = xstrdup (functionname); 321 } 322 discard_last = false; 323 if (linenumber != 0) 324 fprintf (fp, "%s:%u", filename, linenumber); 325 else 326 lfinfo (fp, "%s(%s+0x%v)", filename, section->name, 327 offset); 328 } 329 else if (filename == NULL 330 || strcmp (filename, abfd->filename) == 0) 331 { 332 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, 333 offset); 334 if (linenumber != 0) 335 lfinfo (fp, ":%u", linenumber); 336 } 337 else if (linenumber != 0) 338 lfinfo (fp, "%B:%s:%u", abfd, filename, linenumber); 339 else 340 lfinfo (fp, "%B(%s+0x%v):%s", abfd, section->name, 341 offset, filename); 342 } 343 else 344 lfinfo (fp, "%B(%s+0x%v)", abfd, section->name, offset); 345 346 if (discard_last) 347 { 348 last_bfd = NULL; 349 if (last_file != NULL) 350 { 351 free (last_file); 352 last_file = NULL; 353 } 354 if (last_function != NULL) 355 { 356 free (last_function); 357 last_function = NULL; 358 } 359 } 360 } 361 break; 362 363 case 's': 364 /* arbitrary string, like printf */ 365 fprintf (fp, "%s", va_arg (arg, char *)); 366 break; 367 368 case 'd': 369 /* integer, like printf */ 370 fprintf (fp, "%d", va_arg (arg, int)); 371 break; 372 373 case 'u': 374 /* unsigned integer, like printf */ 375 fprintf (fp, "%u", va_arg (arg, unsigned int)); 376 break; 377 } 378 } 379 } 380 381 if (config.fatal_warnings) 382 config.make_executable = false; 383 384 if (fatal == true) 385 xexit (1); 386} 387 388/* Wrapper around cplus_demangle. Strips leading underscores and 389 other such chars that would otherwise confuse the demangler. */ 390 391char * 392demangle (name) 393 const char *name; 394{ 395 char *res; 396 const char *p; 397 398 if (output_bfd != NULL 399 && bfd_get_symbol_leading_char (output_bfd) == name[0]) 400 ++name; 401 402 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF 403 or the MS PE format. These formats have a number of leading '.'s 404 on at least some symbols, so we remove all dots to avoid 405 confusing the demangler. */ 406 p = name; 407 while (*p == '.') 408 ++p; 409 410 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS); 411 if (res) 412 { 413 size_t dots = p - name; 414 415 /* Now put back any stripped dots. */ 416 if (dots != 0) 417 { 418 size_t len = strlen (res) + 1; 419 char *add_dots = xmalloc (len + dots); 420 421 memcpy (add_dots, name, dots); 422 memcpy (add_dots + dots, res, len); 423 free (res); 424 res = add_dots; 425 } 426 return res; 427 } 428 return xstrdup (name); 429} 430 431/* Format info message and print on stdout. */ 432 433/* (You would think this should be called just "info", but then you 434 would hosed by LynxOS, which defines that name in its libc.) */ 435 436void 437info_msg VPARAMS ((const char *fmt, ...)) 438{ 439 VA_OPEN (arg, fmt); 440 VA_FIXEDARG (arg, const char *, fmt); 441 442 vfinfo (stdout, fmt, arg); 443 VA_CLOSE (arg); 444} 445 446/* ('e' for error.) Format info message and print on stderr. */ 447 448void 449einfo VPARAMS ((const char *fmt, ...)) 450{ 451 VA_OPEN (arg, fmt); 452 VA_FIXEDARG (arg, const char *, fmt); 453 454 vfinfo (stderr, fmt, arg); 455 VA_CLOSE (arg); 456} 457 458void 459info_assert (file, line) 460 const char *file; 461 unsigned int line; 462{ 463 einfo (_("%F%P: internal error %s %d\n"), file, line); 464} 465 466/* ('m' for map) Format info message and print on map. */ 467 468void 469minfo VPARAMS ((const char *fmt, ...)) 470{ 471 VA_OPEN (arg, fmt); 472 VA_FIXEDARG (arg, const char *, fmt); 473 474 vfinfo (config.map_file, fmt, arg); 475 VA_CLOSE (arg); 476} 477 478void 479lfinfo VPARAMS ((FILE *file, const char *fmt, ...)) 480{ 481 VA_OPEN (arg, fmt); 482 VA_FIXEDARG (arg, FILE *, file); 483 VA_FIXEDARG (arg, const char *, fmt); 484 485 vfinfo (file, fmt, arg); 486 VA_CLOSE (arg); 487} 488 489/* Functions to print the link map. */ 490 491void 492print_space () 493{ 494 fprintf (config.map_file, " "); 495} 496 497void 498print_nl () 499{ 500 fprintf (config.map_file, "\n"); 501} 502 503/* A more or less friendly abort message. In ld.h abort is defined to 504 call this function. */ 505 506void 507ld_abort (file, line, fn) 508 const char *file; 509 int line; 510 const char *fn; 511{ 512 if (fn != NULL) 513 einfo (_("%P: internal error: aborting at %s line %d in %s\n"), 514 file, line, fn); 515 else 516 einfo (_("%P: internal error: aborting at %s line %d\n"), 517 file, line); 518 einfo (_("%P%F: please report this bug\n")); 519 xexit (1); 520} 521