Home | History | Annotate | Line # | Download | only in ld
      1   1.1  christos /* ldmisc.c
      2  1.10  christos    Copyright (C) 1991-2025 Free Software Foundation, Inc.
      3   1.1  christos    Written by Steve Chamberlain of Cygnus Support.
      4   1.1  christos 
      5   1.1  christos    This file is part of the GNU Binutils.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program; if not, write to the Free Software
     19   1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20   1.1  christos    MA 02110-1301, USA.  */
     21   1.1  christos 
     22   1.1  christos #include "sysdep.h"
     23   1.1  christos #include "bfd.h"
     24   1.1  christos #include "bfdlink.h"
     25   1.1  christos #include "libiberty.h"
     26   1.7  christos #include "ctf-api.h"
     27   1.6  christos #include "safe-ctype.h"
     28   1.1  christos #include "filenames.h"
     29   1.1  christos #include "demangle.h"
     30   1.1  christos #include <stdarg.h>
     31   1.1  christos #include "ld.h"
     32   1.1  christos #include "ldmisc.h"
     33   1.1  christos #include "ldexp.h"
     34   1.1  christos #include "ldlang.h"
     35   1.1  christos #include <ldgram.h>
     36   1.1  christos #include "ldlex.h"
     37   1.1  christos #include "ldmain.h"
     38   1.1  christos #include "ldfile.h"
     39   1.1  christos 
     40   1.1  christos /*
     41   1.1  christos  %% literal %
     42   1.1  christos  %C clever filename:linenumber with function
     43   1.1  christos  %D like %C, but no function name
     44   1.1  christos  %E current bfd error or errno
     45   1.1  christos  %G like %D, but only function name
     46   1.1  christos  %H like %C but in addition emit section+offset
     47   1.1  christos  %P print program name
     48   1.1  christos  %V hex bfd_vma
     49   1.9  christos  %W hex bfd_vma with 0x with no leading zeros taking up 10 spaces
     50   1.1  christos  %X no object output, fail return
     51   1.1  christos  %d integer, like printf
     52   1.1  christos  %ld long, like printf
     53   1.1  christos  %lu unsigned long, like printf
     54   1.9  christos  %lx unsigned long, like printf
     55   1.1  christos  %p native (host) void* pointer, like printf
     56   1.6  christos  %pA section name from a section
     57   1.6  christos  %pB filename from a bfd
     58   1.6  christos  %pI filename from a lang_input_statement_type
     59   1.6  christos  %pR info about a relent
     60   1.6  christos  %pS print script file and linenumber from etree_type.
     61   1.6  christos  %pT symbol name
     62   1.8  christos  %pU print script file without linenumber from etree_type.
     63   1.1  christos  %s arbitrary string, like printf
     64   1.1  christos  %u integer, like printf
     65   1.1  christos  %v hex bfd_vma, no leading zeros
     66   1.9  christos  %x integer, like printf
     67   1.1  christos */
     68   1.1  christos 
     69   1.1  christos void
     70   1.8  christos vfinfo (FILE *fp, const char *fmt, va_list ap, bool is_warning)
     71   1.1  christos {
     72   1.6  christos   const char *scan;
     73   1.6  christos   int arg_type;
     74   1.6  christos   unsigned int arg_count = 0;
     75   1.6  christos   unsigned int arg_no;
     76   1.6  christos   union vfinfo_args
     77   1.6  christos   {
     78   1.6  christos     int i;
     79   1.6  christos     long l;
     80   1.6  christos     void *p;
     81   1.6  christos     bfd_vma v;
     82   1.6  christos     struct {
     83   1.6  christos       bfd *abfd;
     84   1.6  christos       asection *sec;
     85   1.6  christos       bfd_vma off;
     86   1.6  christos     } reladdr;
     87   1.6  christos     enum
     88   1.6  christos       {
     89   1.6  christos 	Bad,
     90   1.6  christos 	Int,
     91   1.6  christos 	Long,
     92   1.6  christos 	Ptr,
     93   1.6  christos 	Vma,
     94   1.6  christos 	RelAddr
     95   1.6  christos       } type;
     96   1.6  christos   } args[9];
     97   1.6  christos 
     98   1.9  christos   if (is_warning && config.no_warnings)
     99   1.9  christos     return;
    100   1.9  christos 
    101   1.6  christos   for (arg_no = 0; arg_no < sizeof (args) / sizeof (args[0]); arg_no++)
    102   1.6  christos     args[arg_no].type = Bad;
    103   1.6  christos 
    104   1.6  christos   arg_count = 0;
    105   1.6  christos   scan = fmt;
    106   1.6  christos   while (*scan != '\0')
    107   1.6  christos     {
    108   1.6  christos       while (*scan != '%' && *scan != '\0')
    109   1.6  christos 	scan++;
    110   1.6  christos 
    111   1.6  christos       if (*scan == '%')
    112   1.6  christos 	{
    113   1.6  christos 	  scan++;
    114   1.6  christos 
    115   1.6  christos 	  arg_no = arg_count;
    116   1.6  christos 	  if (*scan != '0' && ISDIGIT (*scan) && scan[1] == '$')
    117   1.6  christos 	    {
    118   1.6  christos 	      arg_no = *scan - '1';
    119   1.6  christos 	      scan += 2;
    120   1.6  christos 	    }
    121   1.6  christos 
    122   1.6  christos 	  arg_type = Bad;
    123   1.6  christos 	  switch (*scan++)
    124   1.6  christos 	    {
    125   1.6  christos 	    case '\0':
    126   1.6  christos 	      --scan;
    127   1.6  christos 	      break;
    128   1.6  christos 
    129   1.6  christos 	    case 'V':
    130   1.6  christos 	    case 'v':
    131   1.6  christos 	    case 'W':
    132   1.6  christos 	      arg_type = Vma;
    133   1.6  christos 	      break;
    134   1.6  christos 
    135   1.6  christos 	    case 's':
    136   1.6  christos 	      arg_type = Ptr;
    137   1.6  christos 	      break;
    138   1.6  christos 
    139   1.6  christos 	    case 'p':
    140   1.6  christos 	      if (*scan == 'A' || *scan == 'B' || *scan == 'I'
    141   1.6  christos 		  || *scan == 'R' || *scan == 'S' || *scan ==  'T')
    142   1.6  christos 		scan++;
    143   1.6  christos 	      arg_type = Ptr;
    144   1.6  christos 	      break;
    145   1.6  christos 
    146   1.6  christos 	    case 'C':
    147   1.6  christos 	    case 'D':
    148   1.6  christos 	    case 'G':
    149   1.6  christos 	    case 'H':
    150   1.6  christos 	      arg_type = RelAddr;
    151   1.6  christos 	      break;
    152   1.6  christos 
    153   1.6  christos 	    case 'd':
    154   1.6  christos 	    case 'u':
    155   1.9  christos 	    case 'x':
    156   1.6  christos 	      arg_type = Int;
    157   1.6  christos 	      break;
    158   1.6  christos 
    159   1.6  christos 	    case 'l':
    160   1.9  christos 	      if (*scan == 'd' || *scan == 'u' || *scan == 'x')
    161   1.6  christos 		{
    162   1.6  christos 		  ++scan;
    163   1.6  christos 		  arg_type = Long;
    164   1.6  christos 		}
    165   1.6  christos 	      break;
    166   1.6  christos 
    167   1.6  christos 	    default:
    168   1.6  christos 	      break;
    169   1.6  christos 	    }
    170   1.6  christos 	  if (arg_type != Bad)
    171   1.6  christos 	    {
    172   1.6  christos 	      if (arg_no >= sizeof (args) / sizeof (args[0]))
    173   1.6  christos 		abort ();
    174   1.6  christos 	      args[arg_no].type = arg_type;
    175   1.6  christos 	      ++arg_count;
    176   1.6  christos 	    }
    177   1.6  christos 	}
    178   1.6  christos     }
    179   1.1  christos 
    180   1.6  christos   for (arg_no = 0; arg_no < arg_count; arg_no++)
    181   1.6  christos     {
    182   1.6  christos       switch (args[arg_no].type)
    183   1.6  christos 	{
    184   1.6  christos 	case Int:
    185   1.6  christos 	  args[arg_no].i = va_arg (ap, int);
    186   1.6  christos 	  break;
    187   1.6  christos 	case Long:
    188   1.6  christos 	  args[arg_no].l = va_arg (ap, long);
    189   1.6  christos 	  break;
    190   1.6  christos 	case Ptr:
    191   1.6  christos 	  args[arg_no].p = va_arg (ap, void *);
    192   1.6  christos 	  break;
    193   1.6  christos 	case Vma:
    194   1.6  christos 	  args[arg_no].v = va_arg (ap, bfd_vma);
    195   1.6  christos 	  break;
    196   1.6  christos 	case RelAddr:
    197   1.6  christos 	  args[arg_no].reladdr.abfd = va_arg (ap, bfd *);
    198   1.6  christos 	  args[arg_no].reladdr.sec = va_arg (ap, asection *);
    199   1.6  christos 	  args[arg_no].reladdr.off = va_arg (ap, bfd_vma);
    200   1.6  christos 	  break;
    201   1.6  christos 	default:
    202   1.6  christos 	  abort ();
    203   1.6  christos 	}
    204   1.6  christos     }
    205   1.6  christos 
    206   1.6  christos   arg_count = 0;
    207   1.1  christos   while (*fmt != '\0')
    208   1.1  christos     {
    209   1.1  christos       const char *str = fmt;
    210   1.1  christos       while (*fmt != '%' && *fmt != '\0')
    211   1.1  christos 	fmt++;
    212   1.1  christos       if (fmt != str)
    213   1.1  christos 	if (fwrite (str, 1, fmt - str, fp))
    214   1.1  christos 	  {
    215   1.1  christos 	    /* Ignore.  */
    216   1.1  christos 	  }
    217   1.1  christos 
    218   1.1  christos       if (*fmt == '%')
    219   1.1  christos 	{
    220   1.1  christos 	  fmt++;
    221   1.6  christos 
    222   1.6  christos 	  arg_no = arg_count;
    223   1.6  christos 	  if (*fmt != '0' && ISDIGIT (*fmt) && fmt[1] == '$')
    224   1.6  christos 	    {
    225   1.6  christos 	      arg_no = *fmt - '1';
    226   1.6  christos 	      fmt += 2;
    227   1.6  christos 	    }
    228   1.6  christos 
    229   1.1  christos 	  switch (*fmt++)
    230   1.1  christos 	    {
    231   1.6  christos 	    case '\0':
    232   1.6  christos 	      --fmt;
    233   1.6  christos 	      /* Fall through.  */
    234   1.6  christos 
    235   1.1  christos 	    case '%':
    236   1.1  christos 	      /* literal % */
    237   1.1  christos 	      putc ('%', fp);
    238   1.1  christos 	      break;
    239   1.1  christos 
    240   1.1  christos 	    case 'X':
    241   1.1  christos 	      /* no object output, fail return */
    242   1.8  christos 	      config.make_executable = false;
    243   1.1  christos 	      break;
    244   1.1  christos 
    245   1.1  christos 	    case 'V':
    246   1.1  christos 	      /* hex bfd_vma */
    247   1.1  christos 	      {
    248   1.9  christos 		char buf[32];
    249   1.9  christos 		bfd_vma value;
    250   1.9  christos 
    251   1.9  christos 		value = args[arg_no].v;
    252   1.6  christos 		++arg_count;
    253   1.9  christos 		bfd_sprintf_vma (link_info.output_bfd, buf, value);
    254   1.9  christos 		fprintf (fp, "%s", buf);
    255   1.1  christos 	      }
    256   1.1  christos 	      break;
    257   1.1  christos 
    258   1.1  christos 	    case 'v':
    259   1.1  christos 	      /* hex bfd_vma, no leading zeros */
    260   1.1  christos 	      {
    261   1.9  christos 		uint64_t value = args[arg_no].v;
    262   1.6  christos 		++arg_count;
    263   1.9  christos 		fprintf (fp, "%" PRIx64, value);
    264   1.1  christos 	      }
    265   1.1  christos 	      break;
    266   1.1  christos 
    267   1.1  christos 	    case 'W':
    268   1.1  christos 	      /* hex bfd_vma with 0x with no leading zeroes taking up
    269   1.9  christos 		 10 spaces (including the 0x).  */
    270   1.1  christos 	      {
    271   1.9  christos 		char buf[32];
    272   1.9  christos 		uint64_t value;
    273   1.1  christos 
    274   1.6  christos 		value = args[arg_no].v;
    275   1.6  christos 		++arg_count;
    276   1.9  christos 		sprintf (buf, "0x%" PRIx64, value);
    277   1.9  christos 		fprintf (fp, "%10s", buf);
    278   1.1  christos 	      }
    279   1.1  christos 	      break;
    280   1.1  christos 
    281   1.1  christos 	    case 'P':
    282   1.1  christos 	      /* Print program name.  */
    283   1.1  christos 	      fprintf (fp, "%s", program_name);
    284   1.1  christos 	      break;
    285   1.1  christos 
    286   1.1  christos 	    case 'E':
    287   1.1  christos 	      /* current bfd error or errno */
    288   1.1  christos 	      fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
    289   1.1  christos 	      break;
    290   1.1  christos 
    291   1.1  christos 	    case 'C':
    292   1.1  christos 	    case 'D':
    293   1.1  christos 	    case 'G':
    294   1.1  christos 	    case 'H':
    295   1.1  christos 	      /* Clever filename:linenumber with function name if possible.
    296   1.1  christos 		 The arguments are a BFD, a section, and an offset.  */
    297   1.1  christos 	      {
    298   1.1  christos 		static bfd *last_bfd;
    299   1.6  christos 		static char *last_file;
    300   1.6  christos 		static char *last_function;
    301   1.1  christos 		bfd *abfd;
    302   1.1  christos 		asection *section;
    303   1.1  christos 		bfd_vma offset;
    304   1.1  christos 		asymbol **asymbols = NULL;
    305   1.1  christos 		const char *filename;
    306   1.1  christos 		const char *functionname;
    307   1.1  christos 		unsigned int linenumber;
    308   1.8  christos 		bool discard_last;
    309   1.8  christos 		bool done;
    310   1.7  christos 		bfd_error_type last_bfd_error = bfd_get_error ();
    311   1.1  christos 
    312   1.6  christos 		abfd = args[arg_no].reladdr.abfd;
    313   1.6  christos 		section = args[arg_no].reladdr.sec;
    314   1.6  christos 		offset = args[arg_no].reladdr.off;
    315   1.6  christos 		++arg_count;
    316   1.1  christos 
    317   1.1  christos 		if (abfd != NULL)
    318   1.1  christos 		  {
    319   1.1  christos 		    if (!bfd_generic_link_read_symbols (abfd))
    320  1.10  christos 		      fatal (_("%P: %pB: could not read symbols: %E\n"), abfd);
    321   1.1  christos 
    322   1.1  christos 		    asymbols = bfd_get_outsymbols (abfd);
    323   1.1  christos 		  }
    324   1.1  christos 
    325   1.1  christos 		/* The GNU Coding Standard requires that error messages
    326   1.1  christos 		   be of the form:
    327   1.3  christos 
    328   1.1  christos 		     source-file-name:lineno: message
    329   1.1  christos 
    330   1.1  christos 		   We do not always have a line number available so if
    331   1.1  christos 		   we cannot find them we print out the section name and
    332   1.1  christos 		   offset instead.  */
    333   1.8  christos 		discard_last = true;
    334   1.1  christos 		if (abfd != NULL
    335   1.1  christos 		    && bfd_find_nearest_line (abfd, section, asymbols, offset,
    336   1.1  christos 					      &filename, &functionname,
    337   1.1  christos 					      &linenumber))
    338   1.1  christos 		  {
    339   1.1  christos 		    if (functionname != NULL
    340   1.1  christos 			&& (fmt[-1] == 'C' || fmt[-1] == 'H'))
    341   1.1  christos 		      {
    342   1.1  christos 			/* Detect the case where we are printing out a
    343   1.1  christos 			   message for the same function as the last
    344   1.1  christos 			   call to vinfo ("%C").  In this situation do
    345   1.1  christos 			   not print out the ABFD filename or the
    346   1.1  christos 			   function name again.  Note - we do still
    347   1.1  christos 			   print out the source filename, as this will
    348   1.1  christos 			   allow programs that parse the linker's output
    349   1.1  christos 			   (eg emacs) to correctly locate multiple
    350   1.1  christos 			   errors in the same source file.  */
    351   1.1  christos 			if (last_bfd == NULL
    352   1.1  christos 			    || last_function == NULL
    353   1.1  christos 			    || last_bfd != abfd
    354   1.6  christos 			    || (last_file == NULL) != (filename == NULL)
    355   1.1  christos 			    || (filename != NULL
    356   1.1  christos 				&& filename_cmp (last_file, filename) != 0)
    357   1.1  christos 			    || strcmp (last_function, functionname) != 0)
    358   1.1  christos 			  {
    359   1.6  christos 			    lfinfo (fp, _("%pB: in function `%pT':\n"),
    360   1.1  christos 				    abfd, functionname);
    361   1.1  christos 
    362   1.1  christos 			    last_bfd = abfd;
    363   1.8  christos 			    free (last_file);
    364   1.1  christos 			    last_file = NULL;
    365   1.1  christos 			    if (filename)
    366   1.1  christos 			      last_file = xstrdup (filename);
    367   1.8  christos 			    free (last_function);
    368   1.1  christos 			    last_function = xstrdup (functionname);
    369   1.1  christos 			  }
    370   1.8  christos 			discard_last = false;
    371   1.1  christos 		      }
    372   1.1  christos 		    else
    373   1.6  christos 		      lfinfo (fp, "%pB:", abfd);
    374   1.1  christos 
    375   1.1  christos 		    if (filename != NULL)
    376   1.1  christos 		      fprintf (fp, "%s:", filename);
    377   1.1  christos 
    378   1.1  christos 		    done = fmt[-1] != 'H';
    379   1.1  christos 		    if (functionname != NULL && fmt[-1] == 'G')
    380   1.6  christos 		      lfinfo (fp, "%pT", functionname);
    381   1.1  christos 		    else if (filename != NULL && linenumber != 0)
    382   1.3  christos 		      fprintf (fp, "%u%s", linenumber, done ? "" : ":");
    383   1.1  christos 		    else
    384   1.8  christos 		      done = false;
    385   1.1  christos 		  }
    386   1.1  christos 		else
    387   1.1  christos 		  {
    388   1.6  christos 		    lfinfo (fp, "%pB:", abfd);
    389   1.8  christos 		    done = false;
    390   1.1  christos 		  }
    391   1.1  christos 		if (!done)
    392   1.6  christos 		  lfinfo (fp, "(%pA+0x%v)", section, offset);
    393   1.7  christos 		bfd_set_error (last_bfd_error);
    394   1.1  christos 
    395   1.1  christos 		if (discard_last)
    396   1.1  christos 		  {
    397   1.1  christos 		    last_bfd = NULL;
    398   1.8  christos 		    free (last_file);
    399   1.8  christos 		    last_file = NULL;
    400   1.8  christos 		    free (last_function);
    401   1.8  christos 		    last_function = NULL;
    402   1.1  christos 		  }
    403   1.1  christos 	      }
    404   1.1  christos 	      break;
    405   1.1  christos 
    406   1.1  christos 	    case 'p':
    407   1.6  christos 	      if (*fmt == 'A')
    408   1.6  christos 		{
    409   1.6  christos 		  /* section name from a section */
    410   1.6  christos 		  asection *sec;
    411   1.6  christos 		  bfd *abfd;
    412   1.6  christos 
    413   1.6  christos 		  fmt++;
    414   1.6  christos 		  sec = (asection *) args[arg_no].p;
    415   1.6  christos 		  ++arg_count;
    416   1.7  christos 		  fprintf (fp, "%s", sec->name);
    417   1.6  christos 		  abfd = sec->owner;
    418   1.7  christos 		  if (abfd != NULL)
    419   1.7  christos 		    {
    420   1.7  christos 		      const char *group = bfd_group_name (abfd, sec);
    421   1.7  christos 		      if (group != NULL)
    422   1.7  christos 			fprintf (fp, "[%s]", group);
    423   1.7  christos 		    }
    424   1.6  christos 		}
    425   1.6  christos 	      else if (*fmt == 'B')
    426   1.6  christos 		{
    427   1.6  christos 		  /* filename from a bfd */
    428   1.6  christos 		  bfd *abfd = (bfd *) args[arg_no].p;
    429   1.6  christos 
    430   1.6  christos 		  fmt++;
    431   1.6  christos 		  ++arg_count;
    432   1.6  christos 		  if (abfd == NULL)
    433   1.6  christos 		    fprintf (fp, "%s generated", program_name);
    434   1.6  christos 		  else if (abfd->my_archive != NULL
    435   1.6  christos 			   && !bfd_is_thin_archive (abfd->my_archive))
    436   1.8  christos 		    fprintf (fp, "%s(%s)",
    437   1.8  christos 			     bfd_get_filename (abfd->my_archive),
    438   1.8  christos 			     bfd_get_filename (abfd));
    439   1.6  christos 		  else
    440   1.8  christos 		    fprintf (fp, "%s", bfd_get_filename (abfd));
    441   1.6  christos 		}
    442   1.6  christos 	      else if (*fmt == 'I')
    443   1.6  christos 		{
    444   1.6  christos 		  /* filename from a lang_input_statement_type */
    445   1.6  christos 		  lang_input_statement_type *i;
    446   1.6  christos 
    447   1.6  christos 		  fmt++;
    448   1.6  christos 		  i = (lang_input_statement_type *) args[arg_no].p;
    449   1.6  christos 		  ++arg_count;
    450   1.7  christos 		  if (i->the_bfd != NULL
    451   1.7  christos 		      && i->the_bfd->my_archive != NULL
    452   1.6  christos 		      && !bfd_is_thin_archive (i->the_bfd->my_archive))
    453   1.8  christos 		    fprintf (fp, "(%s)%s",
    454   1.8  christos 			     bfd_get_filename (i->the_bfd->my_archive),
    455   1.7  christos 			     i->local_sym_name);
    456   1.7  christos 		  else
    457   1.7  christos 		    fprintf (fp, "%s", i->filename);
    458   1.6  christos 		}
    459   1.6  christos 	      else if (*fmt == 'R')
    460   1.6  christos 		{
    461   1.6  christos 		  /* Print all that's interesting about a relent.  */
    462   1.6  christos 		  arelent *relent = (arelent *) args[arg_no].p;
    463   1.6  christos 
    464   1.6  christos 		  fmt++;
    465   1.6  christos 		  ++arg_count;
    466   1.6  christos 		  lfinfo (fp, "%s+0x%v (type %s)",
    467   1.6  christos 			  (*(relent->sym_ptr_ptr))->name,
    468   1.6  christos 			  relent->addend,
    469   1.6  christos 			  relent->howto->name);
    470   1.6  christos 		}
    471   1.8  christos 	      else if (*fmt == 'S' || *fmt == 'U')
    472   1.6  christos 		{
    473   1.8  christos 		  /* Print script file and perhaps the associated linenumber.  */
    474   1.6  christos 		  etree_type node;
    475   1.6  christos 		  etree_type *tp = (etree_type *) args[arg_no].p;
    476   1.6  christos 
    477   1.6  christos 		  fmt++;
    478   1.6  christos 		  ++arg_count;
    479   1.6  christos 		  if (tp == NULL)
    480   1.6  christos 		    {
    481   1.6  christos 		      tp = &node;
    482   1.6  christos 		      tp->type.filename = ldlex_filename ();
    483   1.6  christos 		      tp->type.lineno = lineno;
    484   1.6  christos 		    }
    485   1.8  christos 		  if (tp->type.filename != NULL && fmt[-1] == 'S')
    486   1.6  christos 		    fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
    487   1.8  christos 		  else if (tp->type.filename != NULL && fmt[-1] == 'U')
    488   1.8  christos 		    fprintf (fp, "%s", tp->type.filename);
    489   1.6  christos 		}
    490   1.6  christos 	      else if (*fmt == 'T')
    491   1.6  christos 		{
    492   1.6  christos 		  /* Symbol name.  */
    493   1.6  christos 		  const char *name = (const char *) args[arg_no].p;
    494   1.6  christos 
    495   1.6  christos 		  fmt++;
    496   1.6  christos 		  ++arg_count;
    497   1.6  christos 		  if (name == NULL || *name == 0)
    498   1.6  christos 		    {
    499   1.6  christos 		      fprintf (fp, _("no symbol"));
    500   1.6  christos 		      break;
    501   1.6  christos 		    }
    502   1.6  christos 		  else if (demangling)
    503   1.6  christos 		    {
    504   1.6  christos 		      char *demangled;
    505   1.6  christos 
    506   1.6  christos 		      demangled = bfd_demangle (link_info.output_bfd, name,
    507   1.6  christos 						DMGL_ANSI | DMGL_PARAMS);
    508   1.6  christos 		      if (demangled != NULL)
    509   1.6  christos 			{
    510   1.6  christos 			  fprintf (fp, "%s", demangled);
    511   1.6  christos 			  free (demangled);
    512   1.6  christos 			  break;
    513   1.6  christos 			}
    514   1.6  christos 		    }
    515   1.6  christos 		  fprintf (fp, "%s", name);
    516   1.6  christos 		}
    517   1.6  christos 	      else
    518   1.6  christos 		{
    519   1.6  christos 		  /* native (host) void* pointer, like printf */
    520   1.6  christos 		  fprintf (fp, "%p", args[arg_no].p);
    521   1.6  christos 		  ++arg_count;
    522   1.6  christos 		}
    523   1.1  christos 	      break;
    524   1.1  christos 
    525   1.1  christos 	    case 's':
    526   1.1  christos 	      /* arbitrary string, like printf */
    527   1.6  christos 	      fprintf (fp, "%s", (char *) args[arg_no].p);
    528   1.6  christos 	      ++arg_count;
    529   1.1  christos 	      break;
    530   1.1  christos 
    531   1.1  christos 	    case 'd':
    532   1.1  christos 	      /* integer, like printf */
    533   1.6  christos 	      fprintf (fp, "%d", args[arg_no].i);
    534   1.6  christos 	      ++arg_count;
    535   1.1  christos 	      break;
    536   1.1  christos 
    537   1.1  christos 	    case 'u':
    538   1.1  christos 	      /* unsigned integer, like printf */
    539   1.6  christos 	      fprintf (fp, "%u", args[arg_no].i);
    540   1.6  christos 	      ++arg_count;
    541   1.1  christos 	      break;
    542   1.1  christos 
    543   1.9  christos 	    case 'x':
    544   1.9  christos 	      /* unsigned integer, like printf */
    545   1.9  christos 	      fprintf (fp, "%x", args[arg_no].i);
    546   1.9  christos 	      ++arg_count;
    547   1.9  christos 	      break;
    548   1.9  christos 
    549   1.1  christos 	    case 'l':
    550   1.1  christos 	      if (*fmt == 'd')
    551   1.1  christos 		{
    552   1.6  christos 		  fprintf (fp, "%ld", args[arg_no].l);
    553   1.6  christos 		  ++arg_count;
    554   1.1  christos 		  ++fmt;
    555   1.1  christos 		  break;
    556   1.1  christos 		}
    557   1.1  christos 	      else if (*fmt == 'u')
    558   1.1  christos 		{
    559   1.6  christos 		  fprintf (fp, "%lu", args[arg_no].l);
    560   1.6  christos 		  ++arg_count;
    561   1.1  christos 		  ++fmt;
    562   1.1  christos 		  break;
    563   1.1  christos 		}
    564   1.9  christos 	      else if (*fmt == 'x')
    565   1.9  christos 		{
    566   1.9  christos 		  fprintf (fp, "%lx", args[arg_no].l);
    567   1.9  christos 		  ++arg_count;
    568   1.9  christos 		  ++fmt;
    569   1.9  christos 		  break;
    570   1.9  christos 		}
    571   1.6  christos 	      /* Fallthru */
    572   1.1  christos 
    573   1.1  christos 	    default:
    574   1.1  christos 	      fprintf (fp, "%%%c", fmt[-1]);
    575   1.1  christos 	      break;
    576   1.1  christos 	    }
    577   1.1  christos 	}
    578   1.1  christos     }
    579   1.1  christos 
    580   1.1  christos   if (is_warning && config.fatal_warnings)
    581   1.8  christos     config.make_executable = false;
    582   1.1  christos }
    583   1.1  christos 
    584   1.1  christos /* Format info message and print on stdout.  */
    585   1.1  christos 
    586   1.1  christos /* (You would think this should be called just "info", but then you
    587   1.1  christos    would be hosed by LynxOS, which defines that name in its libc.)  */
    588   1.1  christos 
    589   1.1  christos void
    590   1.1  christos info_msg (const char *fmt, ...)
    591   1.1  christos {
    592   1.1  christos   va_list arg;
    593   1.1  christos 
    594   1.1  christos   va_start (arg, fmt);
    595   1.8  christos   vfinfo (stdout, fmt, arg, false);
    596   1.1  christos   va_end (arg);
    597   1.1  christos }
    598   1.1  christos 
    599   1.1  christos /* ('e' for error.) Format info message and print on stderr.  */
    600   1.1  christos 
    601   1.1  christos void
    602   1.1  christos einfo (const char *fmt, ...)
    603   1.1  christos {
    604   1.1  christos   va_list arg;
    605   1.1  christos 
    606   1.1  christos   fflush (stdout);
    607   1.1  christos   va_start (arg, fmt);
    608   1.8  christos   vfinfo (stderr, fmt, arg, true);
    609   1.1  christos   va_end (arg);
    610   1.1  christos   fflush (stderr);
    611   1.1  christos }
    612   1.1  christos 
    613  1.10  christos /* Fatal error.  */
    614  1.10  christos 
    615  1.10  christos void
    616  1.10  christos fatal (const char *fmt, ...)
    617  1.10  christos {
    618  1.10  christos   va_list arg;
    619  1.10  christos 
    620  1.10  christos   fflush (stdout);
    621  1.10  christos   va_start (arg, fmt);
    622  1.10  christos   vfinfo (stderr, fmt, arg, true);
    623  1.10  christos   va_end (arg);
    624  1.10  christos   fflush (stderr);
    625  1.10  christos   xexit (1);
    626  1.10  christos }
    627  1.10  christos 
    628  1.10  christos /* The buffer size for each command-line option warning.  */
    629  1.10  christos #define CMDLINE_WARNING_SIZE	256
    630  1.10  christos 
    631  1.10  christos /* A linked list of command-line option warnings.  */
    632  1.10  christos 
    633  1.10  christos struct cmdline_warning_list
    634  1.10  christos {
    635  1.10  christos   struct cmdline_warning_list *next;
    636  1.10  christos   char *warning;
    637  1.10  christos };
    638  1.10  christos 
    639  1.10  christos /* The head of the linked list of command-line option warnings.  */
    640  1.10  christos static struct cmdline_warning_list *cmdline_warning_head = NULL;
    641  1.10  christos 
    642  1.10  christos /* The tail of the linked list of command-line option warnings.  */
    643  1.10  christos static struct cmdline_warning_list **cmdline_warning_tail
    644  1.10  christos   = &cmdline_warning_head;
    645  1.10  christos 
    646  1.10  christos /* Queue an unknown command-line option warning.  */
    647  1.10  christos 
    648  1.10  christos void
    649  1.10  christos queue_unknown_cmdline_warning (const char *fmt, ...)
    650  1.10  christos {
    651  1.10  christos   va_list arg;
    652  1.10  christos   struct cmdline_warning_list *warning_ptr
    653  1.10  christos     = xmalloc (sizeof (*warning_ptr));
    654  1.10  christos   warning_ptr->warning = xmalloc (CMDLINE_WARNING_SIZE);
    655  1.10  christos   warning_ptr->next = NULL;
    656  1.10  christos   int written;
    657  1.10  christos 
    658  1.10  christos   va_start (arg, fmt);
    659  1.10  christos   written = vsnprintf (warning_ptr->warning, CMDLINE_WARNING_SIZE, fmt,
    660  1.10  christos 		       arg);
    661  1.10  christos   if (written < 0 || written >= CMDLINE_WARNING_SIZE)
    662  1.10  christos     {
    663  1.10  christos       /* If vsnprintf fails or truncates, output the warning directly.  */
    664  1.10  christos       fflush (stdout);
    665  1.10  christos       va_start (arg, fmt);
    666  1.10  christos       vfinfo (stderr, fmt, arg, true);
    667  1.10  christos       fflush (stderr);
    668  1.10  christos     }
    669  1.10  christos   else
    670  1.10  christos     {
    671  1.10  christos       *cmdline_warning_tail = warning_ptr;
    672  1.10  christos       cmdline_warning_tail = &warning_ptr->next;
    673  1.10  christos     }
    674  1.10  christos   va_end (arg);
    675  1.10  christos }
    676  1.10  christos 
    677  1.10  christos /* Output queued unknown command-line option warnings.  */
    678  1.10  christos 
    679  1.10  christos void
    680  1.10  christos output_unknown_cmdline_warnings (void)
    681  1.10  christos {
    682  1.10  christos   struct cmdline_warning_list *list = cmdline_warning_head;
    683  1.10  christos   struct cmdline_warning_list *next;
    684  1.10  christos   if (list == NULL)
    685  1.10  christos     return;
    686  1.10  christos 
    687  1.10  christos   fflush (stdout);
    688  1.10  christos 
    689  1.10  christos   for (; list != NULL; list = next)
    690  1.10  christos     {
    691  1.10  christos       next = list->next;
    692  1.10  christos       if (config.fatal_warnings)
    693  1.10  christos 	einfo (_("%P: error: unsupported option: %s\n"), list->warning);
    694  1.10  christos       else
    695  1.10  christos 	einfo (_("%P: warning: %s ignored\n"), list->warning);
    696  1.10  christos       free (list->warning);
    697  1.10  christos       free (list);
    698  1.10  christos     }
    699  1.10  christos 
    700  1.10  christos   fflush (stderr);
    701  1.10  christos }
    702  1.10  christos 
    703   1.1  christos void
    704   1.1  christos info_assert (const char *file, unsigned int line)
    705   1.1  christos {
    706  1.10  christos   fatal (_("%P: internal error %s %d\n"), file, line);
    707   1.1  christos }
    708   1.1  christos 
    709   1.1  christos /* ('m' for map) Format info message and print on map.  */
    710   1.1  christos 
    711   1.1  christos void
    712   1.1  christos minfo (const char *fmt, ...)
    713   1.1  christos {
    714   1.1  christos   if (config.map_file != NULL)
    715   1.1  christos     {
    716   1.1  christos       va_list arg;
    717   1.1  christos 
    718   1.1  christos       va_start (arg, fmt);
    719   1.3  christos       if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
    720   1.3  christos 	{
    721   1.3  christos 	  /* Stash info about --as-needed shared libraries.  Print
    722   1.3  christos 	     later so they don't appear intermingled with archive
    723   1.3  christos 	     library info.  */
    724   1.3  christos 	  struct asneeded_minfo *m = xmalloc (sizeof *m);
    725   1.3  christos 
    726   1.3  christos 	  m->next = NULL;
    727   1.3  christos 	  m->soname = va_arg (arg, const char *);
    728   1.3  christos 	  m->ref = va_arg (arg, bfd *);
    729   1.3  christos 	  m->name = va_arg (arg, const char *);
    730   1.3  christos 	  *asneeded_list_tail = m;
    731   1.3  christos 	  asneeded_list_tail = &m->next;
    732   1.3  christos 	}
    733   1.3  christos       else
    734   1.8  christos 	vfinfo (config.map_file, fmt, arg, false);
    735   1.1  christos       va_end (arg);
    736   1.1  christos     }
    737   1.1  christos }
    738   1.1  christos 
    739   1.1  christos void
    740   1.1  christos lfinfo (FILE *file, const char *fmt, ...)
    741   1.1  christos {
    742   1.1  christos   va_list arg;
    743   1.1  christos 
    744   1.1  christos   va_start (arg, fmt);
    745   1.8  christos   vfinfo (file, fmt, arg, false);
    746   1.1  christos   va_end (arg);
    747   1.1  christos }
    748   1.1  christos 
    749   1.1  christos /* Functions to print the link map.  */
    751   1.1  christos 
    752   1.9  christos void
    753   1.1  christos print_spaces (int count)
    754   1.9  christos {
    755   1.1  christos   fprintf (config.map_file, "%*s", count, "");
    756   1.1  christos }
    757   1.1  christos 
    758   1.1  christos void
    759   1.1  christos print_nl (void)
    760   1.1  christos {
    761   1.1  christos   fprintf (config.map_file, "\n");
    762   1.1  christos }
    763   1.1  christos 
    764   1.1  christos /* A more or less friendly abort message.  In ld.h abort is defined to
    765   1.1  christos    call this function.  */
    766   1.1  christos 
    767   1.1  christos void
    768   1.1  christos ld_abort (const char *file, int line, const char *fn)
    769   1.1  christos {
    770   1.3  christos   if (fn != NULL)
    771   1.1  christos     einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
    772   1.1  christos 	   file, line, fn);
    773   1.3  christos   else
    774   1.1  christos     einfo (_("%P: internal error: aborting at %s:%d\n"),
    775  1.10  christos 	   file, line);
    776  1.10  christos   fatal (_("%P: please report this bug\n"));
    777  1.10  christos }
    778  1.10  christos 
    779  1.10  christos /* Decode a hexadecimal character. Return -1 on error. */
    780  1.10  christos static int
    781  1.10  christos hexdecode (char c)
    782  1.10  christos {
    783  1.10  christos   if ('0' <= c && c <= '9')
    784  1.10  christos     return c - '0';
    785  1.10  christos   if ('A' <= c && c <= 'F')
    786  1.10  christos     return c - 'A' + 10;
    787  1.10  christos   if ('a' <= c && c <= 'f')
    788  1.10  christos     return c - 'a' + 10;
    789  1.10  christos   return -1;
    790  1.10  christos }
    791  1.10  christos 
    792  1.10  christos /* Decode a percent and/or %[string] encoded string. dst must be at least
    793  1.10  christos    the same size as src. It can be converted in place.
    794  1.10  christos 
    795  1.10  christos    Following %[string] encodings are supported:
    796  1.10  christos 
    797  1.10  christos    %[comma] for ,
    798  1.10  christos    %[lbrace] for {
    799  1.10  christos    %[quot] for "
    800  1.10  christos    %[rbrace] for }
    801  1.10  christos    %[space] for ' '
    802  1.10  christos 
    803  1.10  christos    The percent decoding behaves the same as Python's urllib.parse.unquote. */
    804  1.10  christos void
    805  1.10  christos percent_decode (const char *src, char *dst)
    806  1.10  christos {
    807  1.10  christos   while (*src != '\0')
    808  1.10  christos     {
    809  1.10  christos       char c = *src++;
    810  1.10  christos       if (c == '%')
    811  1.10  christos 	{
    812  1.10  christos 	  char next1 = *src;
    813  1.10  christos 	  int hex1 = hexdecode (next1);
    814  1.10  christos 	  if (hex1 != -1)
    815  1.10  christos 	    {
    816  1.10  christos 	      int hex2 = hexdecode (*(src + 1));
    817  1.10  christos 	      if (hex2 != -1)
    818  1.10  christos 		{
    819  1.10  christos 		  c = (char) ((hex1 << 4) + hex2);
    820  1.10  christos 		  src += 2;
    821  1.10  christos 		}
    822  1.10  christos 	    }
    823  1.10  christos 	  else if (next1 == '[')
    824  1.10  christos 	    {
    825  1.10  christos 	      if (strncmp (src + 1, "comma]", 6) == 0)
    826  1.10  christos 		{
    827  1.10  christos 		  c = ',';
    828  1.10  christos 		  src += 7;
    829  1.10  christos 		}
    830  1.10  christos 	      else if (strncmp (src + 1, "lbrace]", 7) == 0)
    831  1.10  christos 		{
    832  1.10  christos 		  c = '{';
    833  1.10  christos 		  src += 8;
    834  1.10  christos 		}
    835  1.10  christos 	      else if (strncmp (src + 1, "quot]", 5) == 0)
    836  1.10  christos 		{
    837  1.10  christos 		  c = '"';
    838  1.10  christos 		  src += 6;
    839  1.10  christos 		}
    840  1.10  christos 	      else if (strncmp (src + 1, "rbrace]", 7) == 0)
    841  1.10  christos 		{
    842  1.10  christos 		  c = '}';
    843  1.10  christos 		  src += 8;
    844  1.10  christos 		}
    845  1.10  christos 	      else if (strncmp (src + 1, "space]", 6) == 0)
    846  1.10  christos 		{
    847  1.10  christos 		  c = ' ';
    848  1.10  christos 		  src += 7;
    849  1.10  christos 		}
    850  1.10  christos 	    }
    851  1.10  christos 	}
    852  1.10  christos       *dst++ = c;
    853  1.10  christos     }
    854   1.1  christos   *dst = '\0';
    855                 }
    856