ldmisc.c 12 KB

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