ldmisc.c 15 KB

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