xref: /netbsd-src/external/gpl3/binutils/dist/ld/ldmisc.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* ldmisc.c
2*cb63e24eSchristos    Copyright (C) 1991-2024 Free Software Foundation, Inc.
32a6b7db3Sskrll    Written by Steve Chamberlain of Cygnus Support.
42a6b7db3Sskrll 
52a6b7db3Sskrll    This file is part of the GNU Binutils.
62a6b7db3Sskrll 
72a6b7db3Sskrll    This program is free software; you can redistribute it and/or modify
82a6b7db3Sskrll    it under the terms of the GNU General Public License as published by
92a6b7db3Sskrll    the Free Software Foundation; either version 3 of the License, or
102a6b7db3Sskrll    (at your option) any later version.
112a6b7db3Sskrll 
122a6b7db3Sskrll    This program is distributed in the hope that it will be useful,
132a6b7db3Sskrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
142a6b7db3Sskrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
152a6b7db3Sskrll    GNU General Public License for more details.
162a6b7db3Sskrll 
172a6b7db3Sskrll    You should have received a copy of the GNU General Public License
182a6b7db3Sskrll    along with this program; if not, write to the Free Software
192a6b7db3Sskrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
202a6b7db3Sskrll    MA 02110-1301, USA.  */
212a6b7db3Sskrll 
222a6b7db3Sskrll #include "sysdep.h"
232a6b7db3Sskrll #include "bfd.h"
242a6b7db3Sskrll #include "bfdlink.h"
252a6b7db3Sskrll #include "libiberty.h"
266f4ced0bSchristos #include "ctf-api.h"
27fc4f4269Schristos #include "safe-ctype.h"
28883529b6Schristos #include "filenames.h"
292a6b7db3Sskrll #include "demangle.h"
302a6b7db3Sskrll #include <stdarg.h>
312a6b7db3Sskrll #include "ld.h"
322a6b7db3Sskrll #include "ldmisc.h"
332a6b7db3Sskrll #include "ldexp.h"
342a6b7db3Sskrll #include "ldlang.h"
352a6b7db3Sskrll #include <ldgram.h>
362a6b7db3Sskrll #include "ldlex.h"
372a6b7db3Sskrll #include "ldmain.h"
382a6b7db3Sskrll #include "ldfile.h"
392a6b7db3Sskrll 
402a6b7db3Sskrll /*
412a6b7db3Sskrll  %% literal %
422a6b7db3Sskrll  %C clever filename:linenumber with function
432a6b7db3Sskrll  %D like %C, but no function name
442a6b7db3Sskrll  %E current bfd error or errno
452a6b7db3Sskrll  %F error is fatal
462a6b7db3Sskrll  %G like %D, but only function name
47883529b6Schristos  %H like %C but in addition emit section+offset
482a6b7db3Sskrll  %P print program name
492a6b7db3Sskrll  %V hex bfd_vma
50*cb63e24eSchristos  %W hex bfd_vma with 0x with no leading zeros taking up 10 spaces
512a6b7db3Sskrll  %X no object output, fail return
522a6b7db3Sskrll  %d integer, like printf
532a6b7db3Sskrll  %ld long, like printf
542a6b7db3Sskrll  %lu unsigned long, like printf
55*cb63e24eSchristos  %lx unsigned long, like printf
56be9ac0eaSchristos  %p native (host) void* pointer, like printf
57c1a20988Schristos  %pA section name from a section
58c1a20988Schristos  %pB filename from a bfd
59c1a20988Schristos  %pI filename from a lang_input_statement_type
60c1a20988Schristos  %pR info about a relent
61c1a20988Schristos  %pS print script file and linenumber from etree_type.
62c1a20988Schristos  %pT symbol name
634f645668Schristos  %pU print script file without linenumber from etree_type.
642a6b7db3Sskrll  %s arbitrary string, like printf
652a6b7db3Sskrll  %u integer, like printf
662a6b7db3Sskrll  %v hex bfd_vma, no leading zeros
67*cb63e24eSchristos  %x integer, like printf
682a6b7db3Sskrll */
692a6b7db3Sskrll 
70be9ac0eaSchristos void
vfinfo(FILE * fp,const char * fmt,va_list ap,bool is_warning)714f645668Schristos vfinfo (FILE *fp, const char *fmt, va_list ap, bool is_warning)
722a6b7db3Sskrll {
734f645668Schristos   bool fatal = false;
74fc4f4269Schristos   const char *scan;
75fc4f4269Schristos   int arg_type;
76fc4f4269Schristos   unsigned int arg_count = 0;
77fc4f4269Schristos   unsigned int arg_no;
78fc4f4269Schristos   union vfinfo_args
79fc4f4269Schristos   {
80fc4f4269Schristos     int i;
81fc4f4269Schristos     long l;
82fc4f4269Schristos     void *p;
83fc4f4269Schristos     bfd_vma v;
84fc4f4269Schristos     struct {
85fc4f4269Schristos       bfd *abfd;
86fc4f4269Schristos       asection *sec;
87fc4f4269Schristos       bfd_vma off;
88fc4f4269Schristos     } reladdr;
89fc4f4269Schristos     enum
90fc4f4269Schristos       {
91fc4f4269Schristos 	Bad,
92fc4f4269Schristos 	Int,
93fc4f4269Schristos 	Long,
94fc4f4269Schristos 	Ptr,
95fc4f4269Schristos 	Vma,
96fc4f4269Schristos 	RelAddr
97fc4f4269Schristos       } type;
98fc4f4269Schristos   } args[9];
992a6b7db3Sskrll 
100*cb63e24eSchristos   if (is_warning && config.no_warnings)
101*cb63e24eSchristos     return;
102*cb63e24eSchristos 
103fc4f4269Schristos   for (arg_no = 0; arg_no < sizeof (args) / sizeof (args[0]); arg_no++)
104fc4f4269Schristos     args[arg_no].type = Bad;
105fc4f4269Schristos 
106fc4f4269Schristos   arg_count = 0;
107fc4f4269Schristos   scan = fmt;
108fc4f4269Schristos   while (*scan != '\0')
109fc4f4269Schristos     {
110fc4f4269Schristos       while (*scan != '%' && *scan != '\0')
111fc4f4269Schristos 	scan++;
112fc4f4269Schristos 
113fc4f4269Schristos       if (*scan == '%')
114fc4f4269Schristos 	{
115fc4f4269Schristos 	  scan++;
116fc4f4269Schristos 
117fc4f4269Schristos 	  arg_no = arg_count;
118fc4f4269Schristos 	  if (*scan != '0' && ISDIGIT (*scan) && scan[1] == '$')
119fc4f4269Schristos 	    {
120fc4f4269Schristos 	      arg_no = *scan - '1';
121fc4f4269Schristos 	      scan += 2;
122fc4f4269Schristos 	    }
123fc4f4269Schristos 
124fc4f4269Schristos 	  arg_type = Bad;
125fc4f4269Schristos 	  switch (*scan++)
126fc4f4269Schristos 	    {
127fc4f4269Schristos 	    case '\0':
128fc4f4269Schristos 	      --scan;
129fc4f4269Schristos 	      break;
130fc4f4269Schristos 
131fc4f4269Schristos 	    case 'V':
132fc4f4269Schristos 	    case 'v':
133fc4f4269Schristos 	    case 'W':
134fc4f4269Schristos 	      arg_type = Vma;
135fc4f4269Schristos 	      break;
136fc4f4269Schristos 
137fc4f4269Schristos 	    case 's':
138fc4f4269Schristos 	      arg_type = Ptr;
139fc4f4269Schristos 	      break;
140fc4f4269Schristos 
141c1a20988Schristos 	    case 'p':
142c1a20988Schristos 	      if (*scan == 'A' || *scan == 'B' || *scan == 'I'
143c1a20988Schristos 		  || *scan == 'R' || *scan == 'S' || *scan ==  'T')
144c1a20988Schristos 		scan++;
145c1a20988Schristos 	      arg_type = Ptr;
146c1a20988Schristos 	      break;
147c1a20988Schristos 
148fc4f4269Schristos 	    case 'C':
149fc4f4269Schristos 	    case 'D':
150fc4f4269Schristos 	    case 'G':
151fc4f4269Schristos 	    case 'H':
152fc4f4269Schristos 	      arg_type = RelAddr;
153fc4f4269Schristos 	      break;
154fc4f4269Schristos 
155fc4f4269Schristos 	    case 'd':
156fc4f4269Schristos 	    case 'u':
157*cb63e24eSchristos 	    case 'x':
158fc4f4269Schristos 	      arg_type = Int;
159fc4f4269Schristos 	      break;
160fc4f4269Schristos 
161fc4f4269Schristos 	    case 'l':
162*cb63e24eSchristos 	      if (*scan == 'd' || *scan == 'u' || *scan == 'x')
163fc4f4269Schristos 		{
164fc4f4269Schristos 		  ++scan;
165fc4f4269Schristos 		  arg_type = Long;
166fc4f4269Schristos 		}
167fc4f4269Schristos 	      break;
168fc4f4269Schristos 
169fc4f4269Schristos 	    default:
170fc4f4269Schristos 	      break;
171fc4f4269Schristos 	    }
172fc4f4269Schristos 	  if (arg_type != Bad)
173fc4f4269Schristos 	    {
174fc4f4269Schristos 	      if (arg_no >= sizeof (args) / sizeof (args[0]))
175fc4f4269Schristos 		abort ();
176fc4f4269Schristos 	      args[arg_no].type = arg_type;
177fc4f4269Schristos 	      ++arg_count;
178fc4f4269Schristos 	    }
179fc4f4269Schristos 	}
180fc4f4269Schristos     }
181fc4f4269Schristos 
182fc4f4269Schristos   for (arg_no = 0; arg_no < arg_count; arg_no++)
183fc4f4269Schristos     {
184fc4f4269Schristos       switch (args[arg_no].type)
185fc4f4269Schristos 	{
186fc4f4269Schristos 	case Int:
187fc4f4269Schristos 	  args[arg_no].i = va_arg (ap, int);
188fc4f4269Schristos 	  break;
189fc4f4269Schristos 	case Long:
190fc4f4269Schristos 	  args[arg_no].l = va_arg (ap, long);
191fc4f4269Schristos 	  break;
192fc4f4269Schristos 	case Ptr:
193fc4f4269Schristos 	  args[arg_no].p = va_arg (ap, void *);
194fc4f4269Schristos 	  break;
195fc4f4269Schristos 	case Vma:
196fc4f4269Schristos 	  args[arg_no].v = va_arg (ap, bfd_vma);
197fc4f4269Schristos 	  break;
198fc4f4269Schristos 	case RelAddr:
199fc4f4269Schristos 	  args[arg_no].reladdr.abfd = va_arg (ap, bfd *);
200fc4f4269Schristos 	  args[arg_no].reladdr.sec = va_arg (ap, asection *);
201fc4f4269Schristos 	  args[arg_no].reladdr.off = va_arg (ap, bfd_vma);
202fc4f4269Schristos 	  break;
203fc4f4269Schristos 	default:
204fc4f4269Schristos 	  abort ();
205fc4f4269Schristos 	}
206fc4f4269Schristos     }
207fc4f4269Schristos 
208fc4f4269Schristos   arg_count = 0;
2092a6b7db3Sskrll   while (*fmt != '\0')
2102a6b7db3Sskrll     {
211883529b6Schristos       const char *str = fmt;
2122a6b7db3Sskrll       while (*fmt != '%' && *fmt != '\0')
2132a6b7db3Sskrll 	fmt++;
214883529b6Schristos       if (fmt != str)
215883529b6Schristos 	if (fwrite (str, 1, fmt - str, fp))
216883529b6Schristos 	  {
217883529b6Schristos 	    /* Ignore.  */
2182a6b7db3Sskrll 	  }
2192a6b7db3Sskrll 
2202a6b7db3Sskrll       if (*fmt == '%')
2212a6b7db3Sskrll 	{
2222a6b7db3Sskrll 	  fmt++;
223fc4f4269Schristos 
224fc4f4269Schristos 	  arg_no = arg_count;
225fc4f4269Schristos 	  if (*fmt != '0' && ISDIGIT (*fmt) && fmt[1] == '$')
226fc4f4269Schristos 	    {
227fc4f4269Schristos 	      arg_no = *fmt - '1';
228fc4f4269Schristos 	      fmt += 2;
229fc4f4269Schristos 	    }
230fc4f4269Schristos 
2312a6b7db3Sskrll 	  switch (*fmt++)
2322a6b7db3Sskrll 	    {
233fc4f4269Schristos 	    case '\0':
234fc4f4269Schristos 	      --fmt;
235fc4f4269Schristos 	      /* Fall through.  */
236fc4f4269Schristos 
2372a6b7db3Sskrll 	    case '%':
2382a6b7db3Sskrll 	      /* literal % */
2392a6b7db3Sskrll 	      putc ('%', fp);
2402a6b7db3Sskrll 	      break;
2412a6b7db3Sskrll 
2422a6b7db3Sskrll 	    case 'X':
2432a6b7db3Sskrll 	      /* no object output, fail return */
2444f645668Schristos 	      config.make_executable = false;
2452a6b7db3Sskrll 	      break;
2462a6b7db3Sskrll 
2472a6b7db3Sskrll 	    case 'V':
2482a6b7db3Sskrll 	      /* hex bfd_vma */
2492a6b7db3Sskrll 	      {
250*cb63e24eSchristos 		char buf[32];
251*cb63e24eSchristos 		bfd_vma value;
252*cb63e24eSchristos 
253*cb63e24eSchristos 		value = args[arg_no].v;
254fc4f4269Schristos 		++arg_count;
255*cb63e24eSchristos 		bfd_sprintf_vma (link_info.output_bfd, buf, value);
256*cb63e24eSchristos 		fprintf (fp, "%s", buf);
2572a6b7db3Sskrll 	      }
2582a6b7db3Sskrll 	      break;
2592a6b7db3Sskrll 
2602a6b7db3Sskrll 	    case 'v':
2612a6b7db3Sskrll 	      /* hex bfd_vma, no leading zeros */
2622a6b7db3Sskrll 	      {
263*cb63e24eSchristos 		uint64_t value = args[arg_no].v;
264fc4f4269Schristos 		++arg_count;
265*cb63e24eSchristos 		fprintf (fp, "%" PRIx64, value);
2662a6b7db3Sskrll 	      }
2672a6b7db3Sskrll 	      break;
2682a6b7db3Sskrll 
2692a6b7db3Sskrll 	    case 'W':
2702a6b7db3Sskrll 	      /* hex bfd_vma with 0x with no leading zeroes taking up
271*cb63e24eSchristos 		 10 spaces (including the 0x).  */
2722a6b7db3Sskrll 	      {
273*cb63e24eSchristos 		char buf[32];
274*cb63e24eSchristos 		uint64_t value;
2752a6b7db3Sskrll 
276fc4f4269Schristos 		value = args[arg_no].v;
277fc4f4269Schristos 		++arg_count;
278*cb63e24eSchristos 		sprintf (buf, "0x%" PRIx64, value);
279*cb63e24eSchristos 		fprintf (fp, "%10s", buf);
2802a6b7db3Sskrll 	      }
2812a6b7db3Sskrll 	      break;
2822a6b7db3Sskrll 
2832a6b7db3Sskrll 	    case 'F':
2842a6b7db3Sskrll 	      /* Error is fatal.  */
2854f645668Schristos 	      fatal = true;
2862a6b7db3Sskrll 	      break;
2872a6b7db3Sskrll 
2882a6b7db3Sskrll 	    case 'P':
2892a6b7db3Sskrll 	      /* Print program name.  */
2902a6b7db3Sskrll 	      fprintf (fp, "%s", program_name);
2912a6b7db3Sskrll 	      break;
2922a6b7db3Sskrll 
2932a6b7db3Sskrll 	    case 'E':
2942a6b7db3Sskrll 	      /* current bfd error or errno */
2952a6b7db3Sskrll 	      fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
2962a6b7db3Sskrll 	      break;
2972a6b7db3Sskrll 
2982a6b7db3Sskrll 	    case 'C':
2992a6b7db3Sskrll 	    case 'D':
3002a6b7db3Sskrll 	    case 'G':
301883529b6Schristos 	    case 'H':
3022a6b7db3Sskrll 	      /* Clever filename:linenumber with function name if possible.
3032a6b7db3Sskrll 		 The arguments are a BFD, a section, and an offset.  */
3042a6b7db3Sskrll 	      {
3052a6b7db3Sskrll 		static bfd *last_bfd;
306fc4f4269Schristos 		static char *last_file;
307fc4f4269Schristos 		static char *last_function;
3082a6b7db3Sskrll 		bfd *abfd;
3092a6b7db3Sskrll 		asection *section;
3102a6b7db3Sskrll 		bfd_vma offset;
3112a6b7db3Sskrll 		asymbol **asymbols = NULL;
3122a6b7db3Sskrll 		const char *filename;
3132a6b7db3Sskrll 		const char *functionname;
3142a6b7db3Sskrll 		unsigned int linenumber;
3154f645668Schristos 		bool discard_last;
3164f645668Schristos 		bool done;
3176f4ced0bSchristos 		bfd_error_type last_bfd_error = bfd_get_error ();
3182a6b7db3Sskrll 
319fc4f4269Schristos 		abfd = args[arg_no].reladdr.abfd;
320fc4f4269Schristos 		section = args[arg_no].reladdr.sec;
321fc4f4269Schristos 		offset = args[arg_no].reladdr.off;
322fc4f4269Schristos 		++arg_count;
3232a6b7db3Sskrll 
3242a6b7db3Sskrll 		if (abfd != NULL)
3252a6b7db3Sskrll 		  {
3262a6b7db3Sskrll 		    if (!bfd_generic_link_read_symbols (abfd))
327c1a20988Schristos 		      einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
3282a6b7db3Sskrll 
3292a6b7db3Sskrll 		    asymbols = bfd_get_outsymbols (abfd);
3302a6b7db3Sskrll 		  }
3312a6b7db3Sskrll 
3322a6b7db3Sskrll 		/* The GNU Coding Standard requires that error messages
3332a6b7db3Sskrll 		   be of the form:
3342a6b7db3Sskrll 
3352a6b7db3Sskrll 		     source-file-name:lineno: message
3362a6b7db3Sskrll 
3372a6b7db3Sskrll 		   We do not always have a line number available so if
3382a6b7db3Sskrll 		   we cannot find them we print out the section name and
339883529b6Schristos 		   offset instead.  */
3404f645668Schristos 		discard_last = true;
3412a6b7db3Sskrll 		if (abfd != NULL
3422a6b7db3Sskrll 		    && bfd_find_nearest_line (abfd, section, asymbols, offset,
3432a6b7db3Sskrll 					      &filename, &functionname,
3442a6b7db3Sskrll 					      &linenumber))
3452a6b7db3Sskrll 		  {
346883529b6Schristos 		    if (functionname != NULL
347883529b6Schristos 			&& (fmt[-1] == 'C' || fmt[-1] == 'H'))
3482a6b7db3Sskrll 		      {
3492a6b7db3Sskrll 			/* Detect the case where we are printing out a
3502a6b7db3Sskrll 			   message for the same function as the last
3512a6b7db3Sskrll 			   call to vinfo ("%C").  In this situation do
3522a6b7db3Sskrll 			   not print out the ABFD filename or the
3532a6b7db3Sskrll 			   function name again.  Note - we do still
3542a6b7db3Sskrll 			   print out the source filename, as this will
3552a6b7db3Sskrll 			   allow programs that parse the linker's output
3562a6b7db3Sskrll 			   (eg emacs) to correctly locate multiple
3572a6b7db3Sskrll 			   errors in the same source file.  */
3582a6b7db3Sskrll 			if (last_bfd == NULL
3592a6b7db3Sskrll 			    || last_function == NULL
3602a6b7db3Sskrll 			    || last_bfd != abfd
361fc4f4269Schristos 			    || (last_file == NULL) != (filename == NULL)
3622a6b7db3Sskrll 			    || (filename != NULL
363883529b6Schristos 				&& filename_cmp (last_file, filename) != 0)
3642a6b7db3Sskrll 			    || strcmp (last_function, functionname) != 0)
3652a6b7db3Sskrll 			  {
366c1a20988Schristos 			    lfinfo (fp, _("%pB: in function `%pT':\n"),
3672a6b7db3Sskrll 				    abfd, functionname);
3682a6b7db3Sskrll 
3692a6b7db3Sskrll 			    last_bfd = abfd;
3702a6b7db3Sskrll 			    free (last_file);
3712a6b7db3Sskrll 			    last_file = NULL;
3722a6b7db3Sskrll 			    if (filename)
3732a6b7db3Sskrll 			      last_file = xstrdup (filename);
3742a6b7db3Sskrll 			    free (last_function);
3752a6b7db3Sskrll 			    last_function = xstrdup (functionname);
3762a6b7db3Sskrll 			  }
3774f645668Schristos 			discard_last = false;
3782a6b7db3Sskrll 		      }
3792a6b7db3Sskrll 		    else
380c1a20988Schristos 		      lfinfo (fp, "%pB:", abfd);
3812a6b7db3Sskrll 
3822a6b7db3Sskrll 		    if (filename != NULL)
3832a6b7db3Sskrll 		      fprintf (fp, "%s:", filename);
3842a6b7db3Sskrll 
385883529b6Schristos 		    done = fmt[-1] != 'H';
3862a6b7db3Sskrll 		    if (functionname != NULL && fmt[-1] == 'G')
387c1a20988Schristos 		      lfinfo (fp, "%pT", functionname);
3882a6b7db3Sskrll 		    else if (filename != NULL && linenumber != 0)
3899573673dSchristos 		      fprintf (fp, "%u%s", linenumber, done ? "" : ":");
3902a6b7db3Sskrll 		    else
3914f645668Schristos 		      done = false;
3922a6b7db3Sskrll 		  }
3932a6b7db3Sskrll 		else
394883529b6Schristos 		  {
395c1a20988Schristos 		    lfinfo (fp, "%pB:", abfd);
3964f645668Schristos 		    done = false;
397883529b6Schristos 		  }
398883529b6Schristos 		if (!done)
399c1a20988Schristos 		  lfinfo (fp, "(%pA+0x%v)", section, offset);
4006f4ced0bSchristos 		bfd_set_error (last_bfd_error);
4012a6b7db3Sskrll 
4022a6b7db3Sskrll 		if (discard_last)
4032a6b7db3Sskrll 		  {
4042a6b7db3Sskrll 		    last_bfd = NULL;
4052a6b7db3Sskrll 		    free (last_file);
4062a6b7db3Sskrll 		    last_file = NULL;
4072a6b7db3Sskrll 		    free (last_function);
4082a6b7db3Sskrll 		    last_function = NULL;
4092a6b7db3Sskrll 		  }
4102a6b7db3Sskrll 	      }
4112a6b7db3Sskrll 	      break;
4122a6b7db3Sskrll 
413be9ac0eaSchristos 	    case 'p':
414c1a20988Schristos 	      if (*fmt == 'A')
415c1a20988Schristos 		{
416c1a20988Schristos 		  /* section name from a section */
417c1a20988Schristos 		  asection *sec;
418c1a20988Schristos 		  bfd *abfd;
419c1a20988Schristos 
420c1a20988Schristos 		  fmt++;
421c1a20988Schristos 		  sec = (asection *) args[arg_no].p;
422c1a20988Schristos 		  ++arg_count;
423c1a20988Schristos 		  fprintf (fp, "%s", sec->name);
4246f4ced0bSchristos 		  abfd = sec->owner;
4256f4ced0bSchristos 		  if (abfd != NULL)
4266f4ced0bSchristos 		    {
4276f4ced0bSchristos 		      const char *group = bfd_group_name (abfd, sec);
428c1a20988Schristos 		      if (group != NULL)
429c1a20988Schristos 			fprintf (fp, "[%s]", group);
430c1a20988Schristos 		    }
4316f4ced0bSchristos 		}
432c1a20988Schristos 	      else if (*fmt == 'B')
433c1a20988Schristos 		{
434c1a20988Schristos 		  /* filename from a bfd */
435c1a20988Schristos 		  bfd *abfd = (bfd *) args[arg_no].p;
436c1a20988Schristos 
437c1a20988Schristos 		  fmt++;
438c1a20988Schristos 		  ++arg_count;
439c1a20988Schristos 		  if (abfd == NULL)
440c1a20988Schristos 		    fprintf (fp, "%s generated", program_name);
441c1a20988Schristos 		  else if (abfd->my_archive != NULL
442c1a20988Schristos 			   && !bfd_is_thin_archive (abfd->my_archive))
4434f645668Schristos 		    fprintf (fp, "%s(%s)",
4444f645668Schristos 			     bfd_get_filename (abfd->my_archive),
4454f645668Schristos 			     bfd_get_filename (abfd));
446c1a20988Schristos 		  else
4474f645668Schristos 		    fprintf (fp, "%s", bfd_get_filename (abfd));
448c1a20988Schristos 		}
449c1a20988Schristos 	      else if (*fmt == 'I')
450c1a20988Schristos 		{
451c1a20988Schristos 		  /* filename from a lang_input_statement_type */
452c1a20988Schristos 		  lang_input_statement_type *i;
453c1a20988Schristos 
454c1a20988Schristos 		  fmt++;
455c1a20988Schristos 		  i = (lang_input_statement_type *) args[arg_no].p;
456c1a20988Schristos 		  ++arg_count;
4576f4ced0bSchristos 		  if (i->the_bfd != NULL
4586f4ced0bSchristos 		      && i->the_bfd->my_archive != NULL
459c1a20988Schristos 		      && !bfd_is_thin_archive (i->the_bfd->my_archive))
4604f645668Schristos 		    fprintf (fp, "(%s)%s",
4614f645668Schristos 			     bfd_get_filename (i->the_bfd->my_archive),
4626f4ced0bSchristos 			     i->local_sym_name);
4636f4ced0bSchristos 		  else
4646f4ced0bSchristos 		    fprintf (fp, "%s", i->filename);
465c1a20988Schristos 		}
466c1a20988Schristos 	      else if (*fmt == 'R')
467c1a20988Schristos 		{
468c1a20988Schristos 		  /* Print all that's interesting about a relent.  */
469c1a20988Schristos 		  arelent *relent = (arelent *) args[arg_no].p;
470c1a20988Schristos 
471c1a20988Schristos 		  fmt++;
472c1a20988Schristos 		  ++arg_count;
473c1a20988Schristos 		  lfinfo (fp, "%s+0x%v (type %s)",
474c1a20988Schristos 			  (*(relent->sym_ptr_ptr))->name,
475c1a20988Schristos 			  relent->addend,
476c1a20988Schristos 			  relent->howto->name);
477c1a20988Schristos 		}
4784f645668Schristos 	      else if (*fmt == 'S' || *fmt == 'U')
479c1a20988Schristos 		{
4804f645668Schristos 		  /* Print script file and perhaps the associated linenumber.  */
481c1a20988Schristos 		  etree_type node;
482c1a20988Schristos 		  etree_type *tp = (etree_type *) args[arg_no].p;
483c1a20988Schristos 
484c1a20988Schristos 		  fmt++;
485c1a20988Schristos 		  ++arg_count;
486c1a20988Schristos 		  if (tp == NULL)
487c1a20988Schristos 		    {
488c1a20988Schristos 		      tp = &node;
489c1a20988Schristos 		      tp->type.filename = ldlex_filename ();
490c1a20988Schristos 		      tp->type.lineno = lineno;
491c1a20988Schristos 		    }
4924f645668Schristos 		  if (tp->type.filename != NULL && fmt[-1] == 'S')
493c1a20988Schristos 		    fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
4944f645668Schristos 		  else if (tp->type.filename != NULL && fmt[-1] == 'U')
4954f645668Schristos 		    fprintf (fp, "%s", tp->type.filename);
496c1a20988Schristos 		}
497c1a20988Schristos 	      else if (*fmt == 'T')
498c1a20988Schristos 		{
499c1a20988Schristos 		  /* Symbol name.  */
500c1a20988Schristos 		  const char *name = (const char *) args[arg_no].p;
501c1a20988Schristos 
502c1a20988Schristos 		  fmt++;
503c1a20988Schristos 		  ++arg_count;
504c1a20988Schristos 		  if (name == NULL || *name == 0)
505c1a20988Schristos 		    {
506c1a20988Schristos 		      fprintf (fp, _("no symbol"));
507c1a20988Schristos 		      break;
508c1a20988Schristos 		    }
509c1a20988Schristos 		  else if (demangling)
510c1a20988Schristos 		    {
511c1a20988Schristos 		      char *demangled;
512c1a20988Schristos 
513c1a20988Schristos 		      demangled = bfd_demangle (link_info.output_bfd, name,
514c1a20988Schristos 						DMGL_ANSI | DMGL_PARAMS);
515c1a20988Schristos 		      if (demangled != NULL)
516c1a20988Schristos 			{
517c1a20988Schristos 			  fprintf (fp, "%s", demangled);
518c1a20988Schristos 			  free (demangled);
519c1a20988Schristos 			  break;
520c1a20988Schristos 			}
521c1a20988Schristos 		    }
522c1a20988Schristos 		  fprintf (fp, "%s", name);
523c1a20988Schristos 		}
524c1a20988Schristos 	      else
525c1a20988Schristos 		{
526be9ac0eaSchristos 		  /* native (host) void* pointer, like printf */
527fc4f4269Schristos 		  fprintf (fp, "%p", args[arg_no].p);
528fc4f4269Schristos 		  ++arg_count;
529c1a20988Schristos 		}
530be9ac0eaSchristos 	      break;
531be9ac0eaSchristos 
5322a6b7db3Sskrll 	    case 's':
5332a6b7db3Sskrll 	      /* arbitrary string, like printf */
534fc4f4269Schristos 	      fprintf (fp, "%s", (char *) args[arg_no].p);
535fc4f4269Schristos 	      ++arg_count;
5362a6b7db3Sskrll 	      break;
5372a6b7db3Sskrll 
5382a6b7db3Sskrll 	    case 'd':
5392a6b7db3Sskrll 	      /* integer, like printf */
540fc4f4269Schristos 	      fprintf (fp, "%d", args[arg_no].i);
541fc4f4269Schristos 	      ++arg_count;
5422a6b7db3Sskrll 	      break;
5432a6b7db3Sskrll 
5442a6b7db3Sskrll 	    case 'u':
5452a6b7db3Sskrll 	      /* unsigned integer, like printf */
546fc4f4269Schristos 	      fprintf (fp, "%u", args[arg_no].i);
547fc4f4269Schristos 	      ++arg_count;
5482a6b7db3Sskrll 	      break;
5492a6b7db3Sskrll 
550*cb63e24eSchristos 	    case 'x':
551*cb63e24eSchristos 	      /* unsigned integer, like printf */
552*cb63e24eSchristos 	      fprintf (fp, "%x", args[arg_no].i);
553*cb63e24eSchristos 	      ++arg_count;
554*cb63e24eSchristos 	      break;
555*cb63e24eSchristos 
5562a6b7db3Sskrll 	    case 'l':
5572a6b7db3Sskrll 	      if (*fmt == 'd')
5582a6b7db3Sskrll 		{
559fc4f4269Schristos 		  fprintf (fp, "%ld", args[arg_no].l);
560fc4f4269Schristos 		  ++arg_count;
5612a6b7db3Sskrll 		  ++fmt;
5622a6b7db3Sskrll 		  break;
5632a6b7db3Sskrll 		}
5642a6b7db3Sskrll 	      else if (*fmt == 'u')
5652a6b7db3Sskrll 		{
566fc4f4269Schristos 		  fprintf (fp, "%lu", args[arg_no].l);
567fc4f4269Schristos 		  ++arg_count;
5682a6b7db3Sskrll 		  ++fmt;
5692a6b7db3Sskrll 		  break;
5702a6b7db3Sskrll 		}
571*cb63e24eSchristos 	      else if (*fmt == 'x')
572*cb63e24eSchristos 		{
573*cb63e24eSchristos 		  fprintf (fp, "%lx", args[arg_no].l);
574*cb63e24eSchristos 		  ++arg_count;
575*cb63e24eSchristos 		  ++fmt;
576*cb63e24eSchristos 		  break;
577*cb63e24eSchristos 		}
5782a6b7db3Sskrll 	      /* Fallthru */
5792a6b7db3Sskrll 
5802a6b7db3Sskrll 	    default:
5812a6b7db3Sskrll 	      fprintf (fp, "%%%c", fmt[-1]);
5822a6b7db3Sskrll 	      break;
5832a6b7db3Sskrll 	    }
5842a6b7db3Sskrll 	}
5852a6b7db3Sskrll     }
5862a6b7db3Sskrll 
5872a6b7db3Sskrll   if (is_warning && config.fatal_warnings)
5884f645668Schristos     config.make_executable = false;
5892a6b7db3Sskrll 
5902a6b7db3Sskrll   if (fatal)
5912a6b7db3Sskrll     xexit (1);
5922a6b7db3Sskrll }
5932a6b7db3Sskrll 
5942a6b7db3Sskrll /* Format info message and print on stdout.  */
5952a6b7db3Sskrll 
5962a6b7db3Sskrll /* (You would think this should be called just "info", but then you
5972a6b7db3Sskrll    would be hosed by LynxOS, which defines that name in its libc.)  */
5982a6b7db3Sskrll 
5992a6b7db3Sskrll void
info_msg(const char * fmt,...)6002a6b7db3Sskrll info_msg (const char *fmt, ...)
6012a6b7db3Sskrll {
6022a6b7db3Sskrll   va_list arg;
6032a6b7db3Sskrll 
6042a6b7db3Sskrll   va_start (arg, fmt);
6054f645668Schristos   vfinfo (stdout, fmt, arg, false);
6062a6b7db3Sskrll   va_end (arg);
6072a6b7db3Sskrll }
6082a6b7db3Sskrll 
6092a6b7db3Sskrll /* ('e' for error.) Format info message and print on stderr.  */
6102a6b7db3Sskrll 
6112a6b7db3Sskrll void
einfo(const char * fmt,...)6122a6b7db3Sskrll einfo (const char *fmt, ...)
6132a6b7db3Sskrll {
6142a6b7db3Sskrll   va_list arg;
6152a6b7db3Sskrll 
616be9ac0eaSchristos   fflush (stdout);
6172a6b7db3Sskrll   va_start (arg, fmt);
6184f645668Schristos   vfinfo (stderr, fmt, arg, true);
6192a6b7db3Sskrll   va_end (arg);
620be9ac0eaSchristos   fflush (stderr);
6212a6b7db3Sskrll }
6222a6b7db3Sskrll 
6232a6b7db3Sskrll void
info_assert(const char * file,unsigned int line)6242a6b7db3Sskrll info_assert (const char *file, unsigned int line)
6252a6b7db3Sskrll {
6262a6b7db3Sskrll   einfo (_("%F%P: internal error %s %d\n"), file, line);
6272a6b7db3Sskrll }
6282a6b7db3Sskrll 
6292a6b7db3Sskrll /* ('m' for map) Format info message and print on map.  */
6302a6b7db3Sskrll 
6312a6b7db3Sskrll void
minfo(const char * fmt,...)6322a6b7db3Sskrll minfo (const char *fmt, ...)
6332a6b7db3Sskrll {
6342a6b7db3Sskrll   if (config.map_file != NULL)
6352a6b7db3Sskrll     {
6362a6b7db3Sskrll       va_list arg;
6372a6b7db3Sskrll 
6382a6b7db3Sskrll       va_start (arg, fmt);
6399573673dSchristos       if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
6409573673dSchristos 	{
6419573673dSchristos 	  /* Stash info about --as-needed shared libraries.  Print
6429573673dSchristos 	     later so they don't appear intermingled with archive
6439573673dSchristos 	     library info.  */
6449573673dSchristos 	  struct asneeded_minfo *m = xmalloc (sizeof *m);
6459573673dSchristos 
6469573673dSchristos 	  m->next = NULL;
6479573673dSchristos 	  m->soname = va_arg (arg, const char *);
6489573673dSchristos 	  m->ref = va_arg (arg, bfd *);
6499573673dSchristos 	  m->name = va_arg (arg, const char *);
6509573673dSchristos 	  *asneeded_list_tail = m;
6519573673dSchristos 	  asneeded_list_tail = &m->next;
6529573673dSchristos 	}
6539573673dSchristos       else
6544f645668Schristos 	vfinfo (config.map_file, fmt, arg, false);
6552a6b7db3Sskrll       va_end (arg);
6562a6b7db3Sskrll     }
6572a6b7db3Sskrll }
6582a6b7db3Sskrll 
6592a6b7db3Sskrll void
lfinfo(FILE * file,const char * fmt,...)6602a6b7db3Sskrll lfinfo (FILE *file, const char *fmt, ...)
6612a6b7db3Sskrll {
6622a6b7db3Sskrll   va_list arg;
6632a6b7db3Sskrll 
6642a6b7db3Sskrll   va_start (arg, fmt);
6654f645668Schristos   vfinfo (file, fmt, arg, false);
6662a6b7db3Sskrll   va_end (arg);
6672a6b7db3Sskrll }
6682a6b7db3Sskrll 
6692a6b7db3Sskrll /* Functions to print the link map.  */
6702a6b7db3Sskrll 
6712a6b7db3Sskrll void
print_spaces(int count)672*cb63e24eSchristos print_spaces (int count)
6732a6b7db3Sskrll {
674*cb63e24eSchristos   fprintf (config.map_file, "%*s", count, "");
6752a6b7db3Sskrll }
6762a6b7db3Sskrll 
6772a6b7db3Sskrll void
print_nl(void)6782a6b7db3Sskrll print_nl (void)
6792a6b7db3Sskrll {
6802a6b7db3Sskrll   fprintf (config.map_file, "\n");
6812a6b7db3Sskrll }
6822a6b7db3Sskrll 
6832a6b7db3Sskrll /* A more or less friendly abort message.  In ld.h abort is defined to
6842a6b7db3Sskrll    call this function.  */
6852a6b7db3Sskrll 
6862a6b7db3Sskrll void
ld_abort(const char * file,int line,const char * fn)6872a6b7db3Sskrll ld_abort (const char *file, int line, const char *fn)
6882a6b7db3Sskrll {
6892a6b7db3Sskrll   if (fn != NULL)
6909573673dSchristos     einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
6912a6b7db3Sskrll 	   file, line, fn);
6922a6b7db3Sskrll   else
6939573673dSchristos     einfo (_("%P: internal error: aborting at %s:%d\n"),
6942a6b7db3Sskrll 	   file, line);
695c1a20988Schristos   einfo (_("%F%P: please report this bug\n"));
6962a6b7db3Sskrll   xexit (1);
6972a6b7db3Sskrll }
698