Back to index

cell-binutils  2.17cvs20070401
ldmisc.c
Go to the documentation of this file.
00001 /* ldmisc.c
00002    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
00003    2001, 2002, 2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Written by Steve Chamberlain of Cygnus Support.
00006 
00007    This file is part of GLD, the Gnu Linker.
00008 
00009    GLD is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2, or (at your option)
00012    any later version.
00013 
00014    GLD is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with GLD; see the file COPYING.  If not, write to the Free
00021    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
00022    02110-1301, USA.  */
00023 
00024 #include "bfd.h"
00025 #include "bfdlink.h"
00026 #include "sysdep.h"
00027 #include "libiberty.h"
00028 #include "demangle.h"
00029 #include <stdarg.h>
00030 #include "ld.h"
00031 #include "ldmisc.h"
00032 #include "ldexp.h"
00033 #include "ldlang.h"
00034 #include <ldgram.h>
00035 #include "ldlex.h"
00036 #include "ldmain.h"
00037 #include "ldfile.h"
00038 #include "elf-bfd.h"
00039 
00040 /*
00041  %% literal %
00042  %A section name from a section
00043  %B filename from a bfd
00044  %C clever filename:linenumber with function
00045  %D like %C, but no function name
00046  %E current bfd error or errno
00047  %F error is fatal
00048  %G like %D, but only function name
00049  %I filename from a lang_input_statement_type
00050  %P print program name
00051  %R info about a relent
00052  %S print script file and linenumber
00053  %T symbol name
00054  %V hex bfd_vma
00055  %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
00056  %X no object output, fail return
00057  %d integer, like printf
00058  %ld long, like printf
00059  %lu unsigned long, like printf
00060  %s arbitrary string, like printf
00061  %u integer, like printf
00062  %v hex bfd_vma, no leading zeros
00063 */
00064 
00065 static void
00066 vfinfo (FILE *fp, const char *fmt, va_list arg, bfd_boolean is_warning)
00067 {
00068   bfd_boolean fatal = FALSE;
00069 
00070   while (*fmt != '\0')
00071     {
00072       while (*fmt != '%' && *fmt != '\0')
00073        {
00074          putc (*fmt, fp);
00075          fmt++;
00076        }
00077 
00078       if (*fmt == '%')
00079        {
00080          fmt++;
00081          switch (*fmt++)
00082            {
00083            case '%':
00084              /* literal % */
00085              putc ('%', fp);
00086              break;
00087 
00088            case 'X':
00089              /* no object output, fail return */
00090              config.make_executable = FALSE;
00091              break;
00092 
00093            case 'V':
00094              /* hex bfd_vma */
00095              {
00096               bfd_vma value = va_arg (arg, bfd_vma);
00097               fprintf_vma (fp, value);
00098              }
00099              break;
00100 
00101            case 'v':
00102              /* hex bfd_vma, no leading zeros */
00103              {
00104               char buf[100];
00105               char *p = buf;
00106               bfd_vma value = va_arg (arg, bfd_vma);
00107               sprintf_vma (p, value);
00108               while (*p == '0')
00109                 p++;
00110               if (!*p)
00111                 p--;
00112               fputs (p, fp);
00113              }
00114              break;
00115 
00116            case 'W':
00117              /* hex bfd_vma with 0x with no leading zeroes taking up
00118                8 spaces.  */
00119              {
00120               char buf[100];
00121               bfd_vma value;
00122               char *p;
00123               int len;
00124 
00125               value = va_arg (arg, bfd_vma);
00126               sprintf_vma (buf, value);
00127               for (p = buf; *p == '0'; ++p)
00128                 ;
00129               if (*p == '\0')
00130                 --p;
00131               len = strlen (p);
00132               while (len < 8)
00133                 {
00134                   putc (' ', fp);
00135                   ++len;
00136                 }
00137               fprintf (fp, "0x%s", p);
00138              }
00139              break;
00140 
00141            case 'T':
00142              /* Symbol name.  */
00143              {
00144               const char *name = va_arg (arg, const char *);
00145 
00146               if (name == NULL || *name == 0)
00147                 fprintf (fp, _("no symbol"));
00148               else if (! demangling)
00149                 fprintf (fp, "%s", name);
00150               else
00151                 {
00152                   char *demangled;
00153 
00154                   demangled = demangle (name);
00155                   fprintf (fp, "%s", demangled);
00156                   free (demangled);
00157                 }
00158              }
00159              break;
00160 
00161            case 'A':
00162              /* section name from a section */
00163              {
00164               asection *sec = va_arg (arg, asection *);
00165               bfd *abfd = sec->owner;
00166               const char *group = NULL;
00167               struct coff_comdat_info *ci;
00168 
00169               fprintf (fp, "%s", sec->name);
00170               if (abfd != NULL
00171                   && bfd_get_flavour (abfd) == bfd_target_elf_flavour
00172                   && elf_next_in_group (sec) != NULL
00173                   && (sec->flags & SEC_GROUP) == 0)
00174                 group = elf_group_name (sec);
00175               else if (abfd != NULL
00176                       && bfd_get_flavour (abfd) == bfd_target_coff_flavour
00177                       && (ci = bfd_coff_get_comdat_section (sec->owner,
00178                                                         sec)) != NULL)
00179                 group = ci->name;
00180               if (group != NULL)
00181                 fprintf (fp, "[%s]", group);
00182              }
00183              break;
00184 
00185            case 'B':
00186              /* filename from a bfd */
00187              {
00188               bfd *abfd = va_arg (arg, bfd *);
00189 
00190               if (abfd == NULL)
00191                 fprintf (fp, "%s generated", program_name);
00192               else if (abfd->my_archive)
00193                 fprintf (fp, "%s(%s)", abfd->my_archive->filename,
00194                         abfd->filename);
00195               else
00196                 fprintf (fp, "%s", abfd->filename);
00197              }
00198              break;
00199 
00200            case 'F':
00201              /* Error is fatal.  */
00202              fatal = TRUE;
00203              break;
00204 
00205            case 'P':
00206              /* Print program name.  */
00207              fprintf (fp, "%s", program_name);
00208              break;
00209 
00210            case 'E':
00211              /* current bfd error or errno */
00212              fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
00213              break;
00214 
00215            case 'I':
00216              /* filename from a lang_input_statement_type */
00217              {
00218               lang_input_statement_type *i;
00219 
00220               i = va_arg (arg, lang_input_statement_type *);
00221               if (bfd_my_archive (i->the_bfd) != NULL)
00222                 fprintf (fp, "(%s)",
00223                         bfd_get_filename (bfd_my_archive (i->the_bfd)));
00224               fprintf (fp, "%s", i->local_sym_name);
00225               if (bfd_my_archive (i->the_bfd) == NULL
00226                   && strcmp (i->local_sym_name, i->filename) != 0)
00227                 fprintf (fp, " (%s)", i->filename);
00228              }
00229              break;
00230 
00231            case 'S':
00232              /* Print script file and linenumber.  */
00233              if (parsing_defsym)
00234               fprintf (fp, "--defsym %s", lex_string);
00235              else if (ldfile_input_filename != NULL)
00236               fprintf (fp, "%s:%u", ldfile_input_filename, lineno);
00237              else
00238               fprintf (fp, _("built in linker script:%u"), lineno);
00239              break;
00240 
00241            case 'R':
00242              /* Print all that's interesting about a relent.  */
00243              {
00244               arelent *relent = va_arg (arg, arelent *);
00245 
00246               lfinfo (fp, "%s+0x%v (type %s)",
00247                      (*(relent->sym_ptr_ptr))->name,
00248                      relent->addend,
00249                      relent->howto->name);
00250              }
00251              break;
00252 
00253            case 'C':
00254            case 'D':
00255            case 'G':
00256              /* Clever filename:linenumber with function name if possible.
00257                The arguments are a BFD, a section, and an offset.  */
00258              {
00259               static bfd *last_bfd;
00260               static char *last_file = NULL;
00261               static char *last_function = NULL;
00262               bfd *abfd;
00263               asection *section;
00264               bfd_vma offset;
00265               lang_input_statement_type *entry;
00266               asymbol **asymbols;
00267               const char *filename;
00268               const char *functionname;
00269               unsigned int linenumber;
00270               bfd_boolean discard_last;
00271 
00272               abfd = va_arg (arg, bfd *);
00273               section = va_arg (arg, asection *);
00274               offset = va_arg (arg, bfd_vma);
00275 
00276               if (abfd == NULL)
00277                 {
00278                   entry = NULL;
00279                   asymbols = NULL;
00280                 }
00281               else
00282                 {
00283                   entry = (lang_input_statement_type *) abfd->usrdata;
00284                   if (entry != (lang_input_statement_type *) NULL
00285                      && entry->asymbols != (asymbol **) NULL)
00286                     asymbols = entry->asymbols;
00287                   else
00288                     {
00289                      long symsize;
00290                      long sym_count;
00291 
00292                      symsize = bfd_get_symtab_upper_bound (abfd);
00293                      if (symsize < 0)
00294                        einfo (_("%B%F: could not read symbols\n"), abfd);
00295                      asymbols = xmalloc (symsize);
00296                      sym_count = bfd_canonicalize_symtab (abfd, asymbols);
00297                      if (sym_count < 0)
00298                        einfo (_("%B%F: could not read symbols\n"), abfd);
00299                      if (entry != (lang_input_statement_type *) NULL)
00300                        {
00301                          entry->asymbols = asymbols;
00302                          entry->symbol_count = sym_count;
00303                        }
00304                     }
00305                 }
00306 
00307               /* The GNU Coding Standard requires that error messages
00308                  be of the form:
00309                  
00310                    source-file-name:lineno: message
00311 
00312                  We do not always have a line number available so if
00313                  we cannot find them we print out the section name and
00314                  offset instread.  */
00315               discard_last = TRUE;
00316               if (abfd != NULL
00317                   && bfd_find_nearest_line (abfd, section, asymbols, offset,
00318                                          &filename, &functionname,
00319                                          &linenumber))
00320                 {
00321                   if (functionname != NULL && fmt[-1] == 'C')
00322                     {
00323                      /* Detect the case where we are printing out a
00324                         message for the same function as the last
00325                         call to vinfo ("%C").  In this situation do
00326                         not print out the ABFD filename or the
00327                         function name again.  Note - we do still
00328                         print out the source filename, as this will
00329                         allow programs that parse the linker's output
00330                         (eg emacs) to correctly locate multiple
00331                         errors in the same source file.  */
00332                      if (last_bfd == NULL
00333                          || last_file == NULL
00334                          || last_function == NULL
00335                          || last_bfd != abfd
00336                          || (filename != NULL
00337                             && strcmp (last_file, filename) != 0)
00338                          || strcmp (last_function, functionname) != 0)
00339                        {
00340                          lfinfo (fp, _("%B: In function `%T':\n"),
00341                                 abfd, functionname);
00342 
00343                          last_bfd = abfd;
00344                          if (last_file != NULL)
00345                            free (last_file);
00346                          last_file = NULL;
00347                          if (filename)
00348                            last_file = xstrdup (filename);
00349                          if (last_function != NULL)
00350                            free (last_function);
00351                          last_function = xstrdup (functionname);
00352                        }
00353                      discard_last = FALSE;
00354                     }
00355                   else
00356                     lfinfo (fp, "%B:", abfd);
00357 
00358                   if (filename != NULL)
00359                     fprintf (fp, "%s:", filename);
00360 
00361                   if (functionname != NULL && fmt[-1] == 'G')
00362                     lfinfo (fp, "%T", functionname);
00363                   else if (filename != NULL && linenumber != 0)
00364                     fprintf (fp, "%u", linenumber);
00365                   else
00366                     lfinfo (fp, "(%A+0x%v)", section, offset);
00367                 }
00368               else
00369                 lfinfo (fp, "%B:(%A+0x%v)", abfd, section, offset);
00370 
00371               if (asymbols != NULL && entry == NULL)
00372                 free (asymbols);
00373 
00374               if (discard_last)
00375                 {
00376                   last_bfd = NULL;
00377                   if (last_file != NULL)
00378                     {
00379                      free (last_file);
00380                      last_file = NULL;
00381                     }
00382                   if (last_function != NULL)
00383                     {
00384                      free (last_function);
00385                      last_function = NULL;
00386                     }
00387                 }
00388              }
00389              break;
00390 
00391            case 's':
00392              /* arbitrary string, like printf */
00393              fprintf (fp, "%s", va_arg (arg, char *));
00394              break;
00395 
00396            case 'd':
00397              /* integer, like printf */
00398              fprintf (fp, "%d", va_arg (arg, int));
00399              break;
00400 
00401            case 'u':
00402              /* unsigned integer, like printf */
00403              fprintf (fp, "%u", va_arg (arg, unsigned int));
00404              break;
00405 
00406            case 'l':
00407              if (*fmt == 'd')
00408               {
00409                 fprintf (fp, "%ld", va_arg (arg, long));
00410                 ++fmt;
00411                 break;
00412               }
00413              else if (*fmt == 'u')
00414               {
00415                 fprintf (fp, "%lu", va_arg (arg, unsigned long));
00416                 ++fmt;
00417                 break;
00418               }
00419              /* Fall thru */
00420 
00421            default:
00422              fprintf (fp, "%%%c", fmt[-1]);
00423              break;
00424            }
00425        }
00426     }
00427 
00428   if (is_warning && config.fatal_warnings)
00429     config.make_executable = FALSE;
00430 
00431   if (fatal)
00432     xexit (1);
00433 }
00434 
00435 /* Wrapper around cplus_demangle.  Strips leading underscores and
00436    other such chars that would otherwise confuse the demangler.  */
00437 
00438 char *
00439 demangle (const char *name)
00440 {
00441   char *res;
00442   const char *p;
00443 
00444   if (output_bfd != NULL
00445       && bfd_get_symbol_leading_char (output_bfd) == name[0])
00446     ++name;
00447 
00448   /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
00449      or the MS PE format.  These formats have a number of leading '.'s
00450      on at least some symbols, so we remove all dots to avoid
00451      confusing the demangler.  */
00452   p = name;
00453   while (*p == '.')
00454     ++p;
00455 
00456   res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
00457   if (res)
00458     {
00459       size_t dots = p - name;
00460 
00461       /* Now put back any stripped dots.  */
00462       if (dots != 0)
00463        {
00464          size_t len = strlen (res) + 1;
00465          char *add_dots = xmalloc (len + dots);
00466 
00467          memcpy (add_dots, name, dots);
00468          memcpy (add_dots + dots, res, len);
00469          free (res);
00470          res = add_dots;
00471        }
00472       return res;
00473     }
00474   return xstrdup (name);
00475 }
00476 
00477 /* Format info message and print on stdout.  */
00478 
00479 /* (You would think this should be called just "info", but then you
00480    would be hosed by LynxOS, which defines that name in its libc.)  */
00481 
00482 void
00483 info_msg (const char *fmt, ...)
00484 {
00485   va_list arg;
00486 
00487   va_start (arg, fmt);
00488   vfinfo (stdout, fmt, arg, FALSE);
00489   va_end (arg);
00490 }
00491 
00492 /* ('e' for error.) Format info message and print on stderr.  */
00493 
00494 void
00495 einfo (const char *fmt, ...)
00496 {
00497   va_list arg;
00498 
00499   va_start (arg, fmt);
00500   vfinfo (stderr, fmt, arg, TRUE);
00501   va_end (arg);
00502 }
00503 
00504 void
00505 info_assert (const char *file, unsigned int line)
00506 {
00507   einfo (_("%F%P: internal error %s %d\n"), file, line);
00508 }
00509 
00510 /* ('m' for map) Format info message and print on map.  */
00511 
00512 void
00513 minfo (const char *fmt, ...)
00514 {
00515   va_list arg;
00516 
00517   va_start (arg, fmt);
00518   vfinfo (config.map_file, fmt, arg, FALSE);
00519   va_end (arg);
00520 }
00521 
00522 void
00523 lfinfo (FILE *file, const char *fmt, ...)
00524 {
00525   va_list arg;
00526 
00527   va_start (arg, fmt);
00528   vfinfo (file, fmt, arg, FALSE);
00529   va_end (arg);
00530 }
00531 
00532 /* Functions to print the link map.  */
00533 
00534 void
00535 print_space (void)
00536 {
00537   fprintf (config.map_file, " ");
00538 }
00539 
00540 void
00541 print_nl (void)
00542 {
00543   fprintf (config.map_file, "\n");
00544 }
00545 
00546 /* A more or less friendly abort message.  In ld.h abort is defined to
00547    call this function.  */
00548 
00549 void
00550 ld_abort (const char *file, int line, const char *fn)
00551 {
00552   if (fn != NULL)
00553     einfo (_("%P: internal error: aborting at %s line %d in %s\n"),
00554           file, line, fn);
00555   else
00556     einfo (_("%P: internal error: aborting at %s line %d\n"),
00557           file, line);
00558   einfo (_("%P%F: please report this bug\n"));
00559   xexit (1);
00560 }