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