Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ldmisc.c |
4b95cf5c | 2 | Copyright (C) 1991-2014 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Steve Chamberlain of Cygnus Support. |
4 | ||
f96b4a7b | 5 | This file is part of the GNU Binutils. |
252b5132 | 6 | |
f96b4a7b | 7 | This program is free software; you can redistribute it and/or modify |
5ed6aba4 | 8 | it under the terms of the GNU General Public License as published by |
f96b4a7b NC |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
252b5132 | 11 | |
f96b4a7b | 12 | This program is distributed in the hope that it will be useful, |
5ed6aba4 NC |
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. | |
252b5132 | 16 | |
5ed6aba4 | 17 | You should have received a copy of the GNU General Public License |
f96b4a7b NC |
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. */ | |
252b5132 | 21 | |
3db64b00 | 22 | #include "sysdep.h" |
252b5132 | 23 | #include "bfd.h" |
6f6f27f8 | 24 | #include "bfdlink.h" |
252b5132 | 25 | #include "libiberty.h" |
42627821 | 26 | #include "filenames.h" |
252b5132 | 27 | #include "demangle.h" |
252b5132 | 28 | #include <stdarg.h> |
252b5132 RH |
29 | #include "ld.h" |
30 | #include "ldmisc.h" | |
31 | #include "ldexp.h" | |
32 | #include "ldlang.h" | |
df2a7313 | 33 | #include <ldgram.h> |
252b5132 RH |
34 | #include "ldlex.h" |
35 | #include "ldmain.h" | |
36 | #include "ldfile.h" | |
d003868e | 37 | #include "elf-bfd.h" |
252b5132 | 38 | |
252b5132 RH |
39 | /* |
40 | %% literal % | |
d003868e AM |
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 | |
252b5132 | 46 | %F error is fatal |
d003868e | 47 | %G like %D, but only function name |
270396f2 | 48 | %H like %C but in addition emit section+offset |
d003868e | 49 | %I filename from a lang_input_statement_type |
252b5132 | 50 | %P print program name |
d003868e | 51 | %R info about a relent |
dab69f68 | 52 | %S print script file and linenumber from etree_type. |
252b5132 | 53 | %T symbol name |
252b5132 | 54 | %V hex bfd_vma |
252b5132 | 55 | %W hex bfd_vma with 0x with no leading zeros taking up 8 spaces |
d003868e | 56 | %X no object output, fail return |
252b5132 | 57 | %d integer, like printf |
94b50910 AM |
58 | %ld long, like printf |
59 | %lu unsigned long, like printf | |
5d3236ee | 60 | %p native (host) void* pointer, like printf |
d003868e | 61 | %s arbitrary string, like printf |
252b5132 | 62 | %u integer, like printf |
d003868e | 63 | %v hex bfd_vma, no leading zeros |
252b5132 RH |
64 | */ |
65 | ||
5d3236ee | 66 | void |
59ef2528 | 67 | vfinfo (FILE *fp, const char *fmt, va_list arg, bfd_boolean is_warning) |
252b5132 | 68 | { |
b34976b6 | 69 | bfd_boolean fatal = FALSE; |
252b5132 RH |
70 | |
71 | while (*fmt != '\0') | |
72 | { | |
23916fff | 73 | const char *str = fmt; |
6d5e62f8 | 74 | while (*fmt != '%' && *fmt != '\0') |
23916fff MF |
75 | fmt++; |
76 | if (fmt != str) | |
77 | if (fwrite (str, 1, fmt - str, fp)) | |
78 | { | |
79 | /* Ignore. */ | |
80 | } | |
252b5132 | 81 | |
6d5e62f8 | 82 | if (*fmt == '%') |
252b5132 | 83 | { |
6d5e62f8 KH |
84 | fmt++; |
85 | switch (*fmt++) | |
252b5132 | 86 | { |
252b5132 RH |
87 | case '%': |
88 | /* literal % */ | |
89 | putc ('%', fp); | |
90 | break; | |
91 | ||
92 | case 'X': | |
93 | /* no object output, fail return */ | |
b34976b6 | 94 | config.make_executable = FALSE; |
252b5132 RH |
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 | |
1579bae1 | 122 | 8 spaces. */ |
252b5132 RH |
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 | ||
1579bae1 | 150 | if (name == NULL || *name == 0) |
73705ac3 AM |
151 | { |
152 | fprintf (fp, _("no symbol")); | |
153 | break; | |
154 | } | |
155 | else if (demangling) | |
252b5132 RH |
156 | { |
157 | char *demangled; | |
158 | ||
f13a99db | 159 | demangled = bfd_demangle (link_info.output_bfd, name, |
73705ac3 AM |
160 | DMGL_ANSI | DMGL_PARAMS); |
161 | if (demangled != NULL) | |
162 | { | |
163 | fprintf (fp, "%s", demangled); | |
164 | free (demangled); | |
165 | break; | |
166 | } | |
252b5132 | 167 | } |
73705ac3 | 168 | fprintf (fp, "%s", name); |
252b5132 RH |
169 | } |
170 | break; | |
171 | ||
d003868e AM |
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 | ||
252b5132 RH |
196 | case 'B': |
197 | /* filename from a bfd */ | |
6d5e62f8 | 198 | { |
252b5132 | 199 | bfd *abfd = va_arg (arg, bfd *); |
f2763b01 NC |
200 | |
201 | if (abfd == NULL) | |
e1fffbe6 | 202 | fprintf (fp, "%s generated", program_name); |
f2763b01 | 203 | else if (abfd->my_archive) |
252b5132 RH |
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': | |
6d5e62f8 | 212 | /* Error is fatal. */ |
b34976b6 | 213 | fatal = TRUE; |
252b5132 RH |
214 | break; |
215 | ||
216 | case 'P': | |
6d5e62f8 | 217 | /* Print program name. */ |
252b5132 RH |
218 | fprintf (fp, "%s", program_name); |
219 | break; | |
220 | ||
221 | case 'E': | |
222 | /* current bfd error or errno */ | |
305c7206 | 223 | fprintf (fp, "%s", bfd_errmsg (bfd_get_error ())); |
252b5132 RH |
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 | |
42627821 | 237 | && filename_cmp (i->local_sym_name, i->filename) != 0) |
252b5132 RH |
238 | fprintf (fp, " (%s)", i->filename); |
239 | } | |
240 | break; | |
241 | ||
242 | case 'S': | |
6d5e62f8 | 243 | /* Print script file and linenumber. */ |
dab69f68 | 244 | { |
39085894 | 245 | etree_type node; |
dab69f68 AM |
246 | etree_type *tp = va_arg (arg, etree_type *); |
247 | ||
248 | if (tp == NULL) | |
249 | { | |
39085894 | 250 | tp = &node; |
dab69f68 AM |
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 | } | |
252b5132 RH |
257 | break; |
258 | ||
259 | case 'R': | |
6d5e62f8 | 260 | /* Print all that's interesting about a relent. */ |
252b5132 RH |
261 | { |
262 | arelent *relent = va_arg (arg, arelent *); | |
6d5e62f8 | 263 | |
252b5132 RH |
264 | lfinfo (fp, "%s+0x%v (type %s)", |
265 | (*(relent->sym_ptr_ptr))->name, | |
266 | relent->addend, | |
267 | relent->howto->name); | |
268 | } | |
269 | break; | |
6d5e62f8 | 270 | |
252b5132 RH |
271 | case 'C': |
272 | case 'D': | |
273 | case 'G': | |
270396f2 | 274 | case 'H': |
5cfb2bb2 AM |
275 | /* Clever filename:linenumber with function name if possible. |
276 | The arguments are a BFD, a section, and an offset. */ | |
252b5132 RH |
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; | |
5c1d2f5f | 284 | asymbol **asymbols = NULL; |
252b5132 RH |
285 | const char *filename; |
286 | const char *functionname; | |
287 | unsigned int linenumber; | |
b34976b6 | 288 | bfd_boolean discard_last; |
270396f2 | 289 | bfd_boolean done; |
252b5132 RH |
290 | |
291 | abfd = va_arg (arg, bfd *); | |
292 | section = va_arg (arg, asection *); | |
293 | offset = va_arg (arg, bfd_vma); | |
294 | ||
5c1d2f5f | 295 | if (abfd != NULL) |
252b5132 | 296 | { |
5c1d2f5f AM |
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); | |
252b5132 RH |
301 | } |
302 | ||
e1fffbe6 AM |
303 | /* The GNU Coding Standard requires that error messages |
304 | be of the form: | |
e4492aa0 | 305 | |
98d87ee7 | 306 | source-file-name:lineno: message |
5cfb2bb2 | 307 | |
e1fffbe6 AM |
308 | We do not always have a line number available so if |
309 | we cannot find them we print out the section name and | |
270396f2 | 310 | offset instead. */ |
b34976b6 | 311 | discard_last = TRUE; |
e1fffbe6 AM |
312 | if (abfd != NULL |
313 | && bfd_find_nearest_line (abfd, section, asymbols, offset, | |
314 | &filename, &functionname, | |
315 | &linenumber)) | |
252b5132 | 316 | { |
270396f2 AM |
317 | if (functionname != NULL |
318 | && (fmt[-1] == 'C' || fmt[-1] == 'H')) | |
5cfb2bb2 | 319 | { |
e1fffbe6 AM |
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. */ | |
252b5132 RH |
329 | if (last_bfd == NULL |
330 | || last_file == NULL | |
331 | || last_function == NULL | |
332 | || last_bfd != abfd | |
5cfb2bb2 | 333 | || (filename != NULL |
42627821 | 334 | && filename_cmp (last_file, filename) != 0) |
252b5132 RH |
335 | || strcmp (last_function, functionname) != 0) |
336 | { | |
a1c16379 | 337 | lfinfo (fp, _("%B: In function `%T':\n"), |
98d87ee7 | 338 | abfd, functionname); |
252b5132 RH |
339 | |
340 | last_bfd = abfd; | |
341 | if (last_file != NULL) | |
342 | free (last_file); | |
5cfb2bb2 AM |
343 | last_file = NULL; |
344 | if (filename) | |
345 | last_file = xstrdup (filename); | |
252b5132 RH |
346 | if (last_function != NULL) |
347 | free (last_function); | |
d1b2b2dc | 348 | last_function = xstrdup (functionname); |
252b5132 | 349 | } |
b34976b6 | 350 | discard_last = FALSE; |
252b5132 | 351 | } |
98d87ee7 | 352 | else |
a1c16379 | 353 | lfinfo (fp, "%B:", abfd); |
5cfb2bb2 AM |
354 | |
355 | if (filename != NULL) | |
a1c16379 | 356 | fprintf (fp, "%s:", filename); |
5cfb2bb2 | 357 | |
270396f2 | 358 | done = fmt[-1] != 'H'; |
5cfb2bb2 | 359 | if (functionname != NULL && fmt[-1] == 'G') |
a1c16379 JJ |
360 | lfinfo (fp, "%T", functionname); |
361 | else if (filename != NULL && linenumber != 0) | |
18ff9b9b | 362 | fprintf (fp, "%u%s", linenumber, done ? "" : ":"); |
a1c16379 | 363 | else |
270396f2 | 364 | done = FALSE; |
252b5132 | 365 | } |
98d87ee7 | 366 | else |
270396f2 AM |
367 | { |
368 | lfinfo (fp, "%B:", abfd); | |
369 | done = FALSE; | |
370 | } | |
371 | if (!done) | |
372 | lfinfo (fp, "(%A+0x%v)", section, offset); | |
252b5132 RH |
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; | |
6d5e62f8 | 390 | |
5d3236ee DK |
391 | case 'p': |
392 | /* native (host) void* pointer, like printf */ | |
393 | fprintf (fp, "%p", va_arg (arg, void *)); | |
394 | break; | |
395 | ||
252b5132 RH |
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; | |
94b50910 AM |
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; | |
252b5132 RH |
429 | } |
430 | } | |
431 | } | |
432 | ||
59ef2528 | 433 | if (is_warning && config.fatal_warnings) |
b34976b6 | 434 | config.make_executable = FALSE; |
7ce691ae | 435 | |
b34976b6 | 436 | if (fatal) |
6d5e62f8 | 437 | xexit (1); |
252b5132 RH |
438 | } |
439 | ||
6d5e62f8 | 440 | /* Format info message and print on stdout. */ |
252b5132 RH |
441 | |
442 | /* (You would think this should be called just "info", but then you | |
1579bae1 | 443 | would be hosed by LynxOS, which defines that name in its libc.) */ |
252b5132 RH |
444 | |
445 | void | |
1579bae1 | 446 | info_msg (const char *fmt, ...) |
252b5132 | 447 | { |
1579bae1 | 448 | va_list arg; |
252b5132 | 449 | |
1579bae1 | 450 | va_start (arg, fmt); |
59ef2528 | 451 | vfinfo (stdout, fmt, arg, FALSE); |
1579bae1 | 452 | va_end (arg); |
252b5132 RH |
453 | } |
454 | ||
6d5e62f8 | 455 | /* ('e' for error.) Format info message and print on stderr. */ |
252b5132 RH |
456 | |
457 | void | |
1579bae1 | 458 | einfo (const char *fmt, ...) |
252b5132 | 459 | { |
1579bae1 | 460 | va_list arg; |
252b5132 | 461 | |
e922bcab | 462 | fflush (stdout); |
1579bae1 | 463 | va_start (arg, fmt); |
59ef2528 | 464 | vfinfo (stderr, fmt, arg, TRUE); |
1579bae1 | 465 | va_end (arg); |
e922bcab | 466 | fflush (stderr); |
252b5132 RH |
467 | } |
468 | ||
6d5e62f8 | 469 | void |
1579bae1 | 470 | info_assert (const char *file, unsigned int line) |
252b5132 RH |
471 | { |
472 | einfo (_("%F%P: internal error %s %d\n"), file, line); | |
473 | } | |
474 | ||
6d5e62f8 | 475 | /* ('m' for map) Format info message and print on map. */ |
252b5132 RH |
476 | |
477 | void | |
1579bae1 | 478 | minfo (const char *fmt, ...) |
252b5132 | 479 | { |
49fa1e15 AM |
480 | if (config.map_file != NULL) |
481 | { | |
482 | va_list arg; | |
252b5132 | 483 | |
49fa1e15 | 484 | va_start (arg, fmt); |
16e4ecc0 AM |
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); | |
49fa1e15 AM |
501 | va_end (arg); |
502 | } | |
252b5132 RH |
503 | } |
504 | ||
505 | void | |
1579bae1 | 506 | lfinfo (FILE *file, const char *fmt, ...) |
252b5132 | 507 | { |
1579bae1 | 508 | va_list arg; |
252b5132 | 509 | |
1579bae1 | 510 | va_start (arg, fmt); |
59ef2528 | 511 | vfinfo (file, fmt, arg, FALSE); |
1579bae1 | 512 | va_end (arg); |
252b5132 RH |
513 | } |
514 | \f | |
515 | /* Functions to print the link map. */ | |
516 | ||
6d5e62f8 | 517 | void |
1579bae1 | 518 | print_space (void) |
252b5132 RH |
519 | { |
520 | fprintf (config.map_file, " "); | |
521 | } | |
522 | ||
6d5e62f8 | 523 | void |
1579bae1 | 524 | print_nl (void) |
252b5132 RH |
525 | { |
526 | fprintf (config.map_file, "\n"); | |
527 | } | |
45455cdd ILT |
528 | |
529 | /* A more or less friendly abort message. In ld.h abort is defined to | |
530 | call this function. */ | |
531 | ||
532 | void | |
1579bae1 | 533 | ld_abort (const char *file, int line, const char *fn) |
45455cdd ILT |
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 | } |