make score build again
[external/binutils.git] / ld / ldmisc.c
1 /* ldmisc.c
2    Copyright (C) 1991-2016 Free Software Foundation, Inc.
3    Written by Steve Chamberlain of Cygnus Support.
4
5    This file is part of the GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libiberty.h"
26 #include "filenames.h"
27 #include "demangle.h"
28 #include <stdarg.h>
29 #include "ld.h"
30 #include "ldmisc.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include <ldgram.h>
34 #include "ldlex.h"
35 #include "ldmain.h"
36 #include "ldfile.h"
37 #include "elf-bfd.h"
38 #include "coff-bfd.h"
39
40 /*
41  %% literal %
42  %A section name from a section
43  %B filename from a bfd
44  %C clever filename:linenumber with function
45  %D like %C, but no function name
46  %E current bfd error or errno
47  %F error is fatal
48  %G like %D, but only function name
49  %H like %C but in addition emit section+offset
50  %I filename from a lang_input_statement_type
51  %P print program name
52  %R info about a relent
53  %S print script file and linenumber from etree_type.
54  %T symbol name
55  %V hex bfd_vma
56  %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces
57  %X no object output, fail return
58  %d integer, like printf
59  %ld long, like printf
60  %lu unsigned long, like printf
61  %p native (host) void* pointer, like printf
62  %s arbitrary string, like printf
63  %u integer, like printf
64  %v hex bfd_vma, no leading zeros
65 */
66
67 void
68 vfinfo (FILE *fp, const char *fmt, va_list arg, bfd_boolean is_warning)
69 {
70   bfd_boolean fatal = FALSE;
71
72   while (*fmt != '\0')
73     {
74       const char *str = fmt;
75       while (*fmt != '%' && *fmt != '\0')
76         fmt++;
77       if (fmt != str)
78         if (fwrite (str, 1, fmt - str, fp))
79           {
80             /* Ignore.  */
81           }
82
83       if (*fmt == '%')
84         {
85           fmt++;
86           switch (*fmt++)
87             {
88             case '%':
89               /* literal % */
90               putc ('%', fp);
91               break;
92
93             case 'X':
94               /* no object output, fail return */
95               config.make_executable = FALSE;
96               break;
97
98             case 'V':
99               /* hex bfd_vma */
100               {
101                 bfd_vma value = va_arg (arg, bfd_vma);
102                 fprintf_vma (fp, value);
103               }
104               break;
105
106             case 'v':
107               /* hex bfd_vma, no leading zeros */
108               {
109                 char buf[100];
110                 char *p = buf;
111                 bfd_vma value = va_arg (arg, bfd_vma);
112                 sprintf_vma (p, value);
113                 while (*p == '0')
114                   p++;
115                 if (!*p)
116                   p--;
117                 fputs (p, fp);
118               }
119               break;
120
121             case 'W':
122               /* hex bfd_vma with 0x with no leading zeroes taking up
123                  8 spaces.  */
124               {
125                 char buf[100];
126                 bfd_vma value;
127                 char *p;
128                 int len;
129
130                 value = va_arg (arg, bfd_vma);
131                 sprintf_vma (buf, value);
132                 for (p = buf; *p == '0'; ++p)
133                   ;
134                 if (*p == '\0')
135                   --p;
136                 len = strlen (p);
137                 while (len < 8)
138                   {
139                     putc (' ', fp);
140                     ++len;
141                   }
142                 fprintf (fp, "0x%s", p);
143               }
144               break;
145
146             case 'T':
147               /* Symbol name.  */
148               {
149                 const char *name = va_arg (arg, const char *);
150
151                 if (name == NULL || *name == 0)
152                   {
153                     fprintf (fp, _("no symbol"));
154                     break;
155                   }
156                 else if (demangling)
157                   {
158                     char *demangled;
159
160                     demangled = bfd_demangle (link_info.output_bfd, name,
161                                               DMGL_ANSI | DMGL_PARAMS);
162                     if (demangled != NULL)
163                       {
164                         fprintf (fp, "%s", demangled);
165                         free (demangled);
166                         break;
167                       }
168                   }
169                 fprintf (fp, "%s", name);
170               }
171               break;
172
173             case 'A':
174               /* section name from a section */
175               {
176                 asection *sec = va_arg (arg, asection *);
177                 bfd *abfd = sec->owner;
178                 const char *group = NULL;
179                 struct coff_comdat_info *ci;
180
181                 fprintf (fp, "%s", sec->name);
182                 if (abfd != NULL
183                     && bfd_get_flavour (abfd) == bfd_target_elf_flavour
184                     && elf_next_in_group (sec) != NULL
185                     && (sec->flags & SEC_GROUP) == 0)
186                   group = elf_group_name (sec);
187                 else if (abfd != NULL
188                          && bfd_get_flavour (abfd) == bfd_target_coff_flavour
189                          && (ci = bfd_coff_get_comdat_section (sec->owner,
190                                                                sec)) != NULL)
191                   group = ci->name;
192                 if (group != NULL)
193                   fprintf (fp, "[%s]", group);
194               }
195               break;
196
197             case 'B':
198               /* filename from a bfd */
199               {
200                 bfd *abfd = va_arg (arg, bfd *);
201
202                 if (abfd == NULL)
203                   fprintf (fp, "%s generated", program_name);
204                 else if (abfd->my_archive)
205                   fprintf (fp, "%s(%s)", abfd->my_archive->filename,
206                            abfd->filename);
207                 else
208                   fprintf (fp, "%s", abfd->filename);
209               }
210               break;
211
212             case 'F':
213               /* Error is fatal.  */
214               fatal = TRUE;
215               break;
216
217             case 'P':
218               /* Print program name.  */
219               fprintf (fp, "%s", program_name);
220               break;
221
222             case 'E':
223               /* current bfd error or errno */
224               fprintf (fp, "%s", bfd_errmsg (bfd_get_error ()));
225               break;
226
227             case 'I':
228               /* filename from a lang_input_statement_type */
229               {
230                 lang_input_statement_type *i;
231
232                 i = va_arg (arg, lang_input_statement_type *);
233                 if (bfd_my_archive (i->the_bfd) != NULL)
234                   fprintf (fp, "(%s)",
235                            bfd_get_filename (bfd_my_archive (i->the_bfd)));
236                 fprintf (fp, "%s", i->local_sym_name);
237                 if (bfd_my_archive (i->the_bfd) == NULL
238                     && filename_cmp (i->local_sym_name, i->filename) != 0)
239                   fprintf (fp, " (%s)", i->filename);
240               }
241               break;
242
243             case 'S':
244               /* Print script file and linenumber.  */
245               {
246                 etree_type node;
247                 etree_type *tp = va_arg (arg, etree_type *);
248
249                 if (tp == NULL)
250                   {
251                     tp = &node;
252                     tp->type.filename = ldlex_filename ();
253                     tp->type.lineno = lineno;
254                   }
255                 if (tp->type.filename != NULL)
256                   fprintf (fp, "%s:%u", tp->type.filename, tp->type.lineno);
257               }
258               break;
259
260             case 'R':
261               /* Print all that's interesting about a relent.  */
262               {
263                 arelent *relent = va_arg (arg, arelent *);
264
265                 lfinfo (fp, "%s+0x%v (type %s)",
266                         (*(relent->sym_ptr_ptr))->name,
267                         relent->addend,
268                         relent->howto->name);
269               }
270               break;
271
272             case 'C':
273             case 'D':
274             case 'G':
275             case 'H':
276               /* Clever filename:linenumber with function name if possible.
277                  The arguments are a BFD, a section, and an offset.  */
278               {
279                 static bfd *last_bfd;
280                 static char *last_file = NULL;
281                 static char *last_function = NULL;
282                 bfd *abfd;
283                 asection *section;
284                 bfd_vma offset;
285                 asymbol **asymbols = NULL;
286                 const char *filename;
287                 const char *functionname;
288                 unsigned int linenumber;
289                 bfd_boolean discard_last;
290                 bfd_boolean done;
291
292                 abfd = va_arg (arg, bfd *);
293                 section = va_arg (arg, asection *);
294                 offset = va_arg (arg, bfd_vma);
295
296                 if (abfd != NULL)
297                   {
298                     if (!bfd_generic_link_read_symbols (abfd))
299                       einfo (_("%B%F: could not read symbols: %E\n"), abfd);
300
301                     asymbols = bfd_get_outsymbols (abfd);
302                   }
303
304                 /* The GNU Coding Standard requires that error messages
305                    be of the form:
306
307                      source-file-name:lineno: message
308
309                    We do not always have a line number available so if
310                    we cannot find them we print out the section name and
311                    offset instead.  */
312                 discard_last = TRUE;
313                 if (abfd != NULL
314                     && bfd_find_nearest_line (abfd, section, asymbols, offset,
315                                               &filename, &functionname,
316                                               &linenumber))
317                   {
318                     if (functionname != NULL
319                         && (fmt[-1] == 'C' || fmt[-1] == 'H'))
320                       {
321                         /* Detect the case where we are printing out a
322                            message for the same function as the last
323                            call to vinfo ("%C").  In this situation do
324                            not print out the ABFD filename or the
325                            function name again.  Note - we do still
326                            print out the source filename, as this will
327                            allow programs that parse the linker's output
328                            (eg emacs) to correctly locate multiple
329                            errors in the same source file.  */
330                         if (last_bfd == NULL
331                             || last_file == NULL
332                             || last_function == NULL
333                             || last_bfd != abfd
334                             || (filename != NULL
335                                 && filename_cmp (last_file, filename) != 0)
336                             || strcmp (last_function, functionname) != 0)
337                           {
338                             lfinfo (fp, _("%B: In function `%T':\n"),
339                                     abfd, functionname);
340
341                             last_bfd = abfd;
342                             if (last_file != NULL)
343                               free (last_file);
344                             last_file = NULL;
345                             if (filename)
346                               last_file = xstrdup (filename);
347                             if (last_function != NULL)
348                               free (last_function);
349                             last_function = xstrdup (functionname);
350                           }
351                         discard_last = FALSE;
352                       }
353                     else
354                       lfinfo (fp, "%B:", abfd);
355
356                     if (filename != NULL)
357                       fprintf (fp, "%s:", filename);
358
359                     done = fmt[-1] != 'H';
360                     if (functionname != NULL && fmt[-1] == 'G')
361                       lfinfo (fp, "%T", functionname);
362                     else if (filename != NULL && linenumber != 0)
363                       fprintf (fp, "%u%s", linenumber, done ? "" : ":");
364                     else
365                       done = FALSE;
366                   }
367                 else
368                   {
369                     lfinfo (fp, "%B:", abfd);
370                     done = FALSE;
371                   }
372                 if (!done)
373                   lfinfo (fp, "(%A+0x%v)", section, offset);
374
375                 if (discard_last)
376                   {
377                     last_bfd = NULL;
378                     if (last_file != NULL)
379                       {
380                         free (last_file);
381                         last_file = NULL;
382                       }
383                     if (last_function != NULL)
384                       {
385                         free (last_function);
386                         last_function = NULL;
387                       }
388                   }
389               }
390               break;
391
392             case 'p':
393               /* native (host) void* pointer, like printf */
394               fprintf (fp, "%p", va_arg (arg, void *));
395               break;
396
397             case 's':
398               /* arbitrary string, like printf */
399               fprintf (fp, "%s", va_arg (arg, char *));
400               break;
401
402             case 'd':
403               /* integer, like printf */
404               fprintf (fp, "%d", va_arg (arg, int));
405               break;
406
407             case 'u':
408               /* unsigned integer, like printf */
409               fprintf (fp, "%u", va_arg (arg, unsigned int));
410               break;
411
412             case 'l':
413               if (*fmt == 'd')
414                 {
415                   fprintf (fp, "%ld", va_arg (arg, long));
416                   ++fmt;
417                   break;
418                 }
419               else if (*fmt == 'u')
420                 {
421                   fprintf (fp, "%lu", va_arg (arg, unsigned long));
422                   ++fmt;
423                   break;
424                 }
425               /* Fall thru */
426
427             default:
428               fprintf (fp, "%%%c", fmt[-1]);
429               break;
430             }
431         }
432     }
433
434   if (is_warning && config.fatal_warnings)
435     config.make_executable = FALSE;
436
437   if (fatal)
438     xexit (1);
439 }
440
441 /* Format info message and print on stdout.  */
442
443 /* (You would think this should be called just "info", but then you
444    would be hosed by LynxOS, which defines that name in its libc.)  */
445
446 void
447 info_msg (const char *fmt, ...)
448 {
449   va_list arg;
450
451   va_start (arg, fmt);
452   vfinfo (stdout, fmt, arg, FALSE);
453   va_end (arg);
454 }
455
456 /* ('e' for error.) Format info message and print on stderr.  */
457
458 void
459 einfo (const char *fmt, ...)
460 {
461   va_list arg;
462
463   fflush (stdout);
464   va_start (arg, fmt);
465   vfinfo (stderr, fmt, arg, TRUE);
466   va_end (arg);
467   fflush (stderr);
468 }
469
470 void
471 info_assert (const char *file, unsigned int line)
472 {
473   einfo (_("%F%P: internal error %s %d\n"), file, line);
474 }
475
476 /* ('m' for map) Format info message and print on map.  */
477
478 void
479 minfo (const char *fmt, ...)
480 {
481   if (config.map_file != NULL)
482     {
483       va_list arg;
484
485       va_start (arg, fmt);
486       if (fmt[0] == '%' && fmt[1] == '!' && fmt[2] == 0)
487         {
488           /* Stash info about --as-needed shared libraries.  Print
489              later so they don't appear intermingled with archive
490              library info.  */
491           struct asneeded_minfo *m = xmalloc (sizeof *m);
492
493           m->next = NULL;
494           m->soname = va_arg (arg, const char *);
495           m->ref = va_arg (arg, bfd *);
496           m->name = va_arg (arg, const char *);
497           *asneeded_list_tail = m;
498           asneeded_list_tail = &m->next;
499         }
500       else
501         vfinfo (config.map_file, fmt, arg, FALSE);
502       va_end (arg);
503     }
504 }
505
506 void
507 lfinfo (FILE *file, const char *fmt, ...)
508 {
509   va_list arg;
510
511   va_start (arg, fmt);
512   vfinfo (file, fmt, arg, FALSE);
513   va_end (arg);
514 }
515 \f
516 /* Functions to print the link map.  */
517
518 void
519 print_space (void)
520 {
521   fprintf (config.map_file, " ");
522 }
523
524 void
525 print_nl (void)
526 {
527   fprintf (config.map_file, "\n");
528 }
529
530 /* A more or less friendly abort message.  In ld.h abort is defined to
531    call this function.  */
532
533 void
534 ld_abort (const char *file, int line, const char *fn)
535 {
536   if (fn != NULL)
537     einfo (_("%P: internal error: aborting at %s:%d in %s\n"),
538            file, line, fn);
539   else
540     einfo (_("%P: internal error: aborting at %s:%d\n"),
541            file, line);
542   einfo (_("%P%F: please report this bug\n"));
543   xexit (1);
544 }