bucomm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /* bucomm.c -- Bin Utils COMmon code.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* We might put this in a library someday so it could be dynamically
  17. loaded, but for now it's not necessary. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "libiberty.h"
  21. #include "filenames.h"
  22. #include <time.h>
  23. #include <assert.h>
  24. #include "bucomm.h"
  25. /* Error reporting. */
  26. char *program_name;
  27. void
  28. bfd_nonfatal (const char *string)
  29. {
  30. const char *errmsg;
  31. enum bfd_error err = bfd_get_error ();
  32. if (err == bfd_error_no_error)
  33. errmsg = _("cause of error unknown");
  34. else
  35. errmsg = bfd_errmsg (err);
  36. fflush (stdout);
  37. if (string)
  38. fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
  39. else
  40. fprintf (stderr, "%s: %s\n", program_name, errmsg);
  41. }
  42. /* Issue a non fatal error message. FILENAME, or if NULL then BFD,
  43. are used to indicate the problematic file. SECTION, if non NULL,
  44. is used to provide a section name. If FORMAT is non-null, then it
  45. is used to print additional information via vfprintf. Finally the
  46. bfd error message is printed. In summary, error messages are of
  47. one of the following forms:
  48. PROGRAM: file: bfd-error-message
  49. PROGRAM: file[section]: bfd-error-message
  50. PROGRAM: file: printf-message: bfd-error-message
  51. PROGRAM: file[section]: printf-message: bfd-error-message. */
  52. void
  53. bfd_nonfatal_message (const char *filename,
  54. const bfd *abfd,
  55. const asection *section,
  56. const char *format, ...)
  57. {
  58. const char *errmsg;
  59. const char *section_name;
  60. enum bfd_error err = bfd_get_error ();
  61. if (err == bfd_error_no_error)
  62. errmsg = _("cause of error unknown");
  63. else
  64. errmsg = bfd_errmsg (err);
  65. fflush (stdout);
  66. section_name = NULL;
  67. fprintf (stderr, "%s", program_name);
  68. if (abfd)
  69. {
  70. if (!filename)
  71. filename = bfd_get_archive_filename (abfd);
  72. if (section)
  73. section_name = bfd_section_name (section);
  74. }
  75. if (section_name)
  76. fprintf (stderr, ": %s[%s]", filename, section_name);
  77. else
  78. fprintf (stderr, ": %s", filename);
  79. if (format)
  80. {
  81. va_list args;
  82. va_start (args, format);
  83. fprintf (stderr, ": ");
  84. vfprintf (stderr, format, args);
  85. va_end (args);
  86. }
  87. fprintf (stderr, ": %s\n", errmsg);
  88. }
  89. void
  90. bfd_fatal (const char *string)
  91. {
  92. bfd_nonfatal (string);
  93. xexit (1);
  94. }
  95. void
  96. report (const char * format, va_list args)
  97. {
  98. fflush (stdout);
  99. fprintf (stderr, "%s: ", program_name);
  100. vfprintf (stderr, format, args);
  101. putc ('\n', stderr);
  102. }
  103. void
  104. fatal (const char *format, ...)
  105. {
  106. va_list args;
  107. va_start (args, format);
  108. report (format, args);
  109. va_end (args);
  110. xexit (1);
  111. }
  112. void
  113. non_fatal (const char *format, ...)
  114. {
  115. va_list args;
  116. va_start (args, format);
  117. report (format, args);
  118. va_end (args);
  119. }
  120. /* Set the default BFD target based on the configured target. Doing
  121. this permits the binutils to be configured for a particular target,
  122. and linked against a shared BFD library which was configured for a
  123. different target. */
  124. void
  125. set_default_bfd_target (void)
  126. {
  127. /* The macro TARGET is defined by Makefile. */
  128. const char *target = TARGET;
  129. if (! bfd_set_default_target (target))
  130. fatal (_("can't set BFD default target to `%s': %s"),
  131. target, bfd_errmsg (bfd_get_error ()));
  132. }
  133. /* After a FALSE return from bfd_check_format_matches with
  134. bfd_get_error () == bfd_error_file_ambiguously_recognized, print
  135. the possible matching targets. */
  136. void
  137. list_matching_formats (char **p)
  138. {
  139. fflush (stdout);
  140. fprintf (stderr, _("%s: Matching formats:"), program_name);
  141. while (*p)
  142. fprintf (stderr, " %s", *p++);
  143. fputc ('\n', stderr);
  144. }
  145. /* List the supported targets. */
  146. void
  147. list_supported_targets (const char *name, FILE *f)
  148. {
  149. int t;
  150. const char **targ_names;
  151. if (name == NULL)
  152. fprintf (f, _("Supported targets:"));
  153. else
  154. fprintf (f, _("%s: supported targets:"), name);
  155. targ_names = bfd_target_list ();
  156. for (t = 0; targ_names[t] != NULL; t++)
  157. fprintf (f, " %s", targ_names[t]);
  158. fprintf (f, "\n");
  159. free (targ_names);
  160. }
  161. /* List the supported architectures. */
  162. void
  163. list_supported_architectures (const char *name, FILE *f)
  164. {
  165. const char ** arch;
  166. const char ** arches;
  167. if (name == NULL)
  168. fprintf (f, _("Supported architectures:"));
  169. else
  170. fprintf (f, _("%s: supported architectures:"), name);
  171. for (arch = arches = bfd_arch_list (); *arch; arch++)
  172. fprintf (f, " %s", *arch);
  173. fprintf (f, "\n");
  174. free (arches);
  175. }
  176. static const char *
  177. endian_string (enum bfd_endian endian)
  178. {
  179. switch (endian)
  180. {
  181. case BFD_ENDIAN_BIG: return _("big endian");
  182. case BFD_ENDIAN_LITTLE: return _("little endian");
  183. default: return _("endianness unknown");
  184. }
  185. }
  186. /* Data passed to do_display_target and other target iterators. */
  187. struct display_target {
  188. /* Temp file. */
  189. char *filename;
  190. /* Return status. */
  191. int error;
  192. /* Number of targets. */
  193. int count;
  194. /* Size of info in bytes. */
  195. size_t alloc;
  196. /* Per-target info. */
  197. struct {
  198. /* Target name. */
  199. const char *name;
  200. /* Non-zero if target/arch combination supported. */
  201. unsigned char arch[bfd_arch_last - bfd_arch_obscure - 1];
  202. } *info;
  203. };
  204. /* List the targets that BFD is configured to support, each followed
  205. by its endianness and the architectures it supports. Also build
  206. info about target/archs. */
  207. static int
  208. do_display_target (const bfd_target *targ, void *data)
  209. {
  210. struct display_target *param = (struct display_target *) data;
  211. bfd *abfd;
  212. size_t amt;
  213. param->count += 1;
  214. amt = param->count * sizeof (*param->info);
  215. if (param->alloc < amt)
  216. {
  217. size_t size = ((param->count < 64 ? 64 : param->count)
  218. * sizeof (*param->info) * 2);
  219. param->info = xrealloc (param->info, size);
  220. memset ((char *) param->info + param->alloc, 0, size - param->alloc);
  221. param->alloc = size;
  222. }
  223. param->info[param->count - 1].name = targ->name;
  224. printf (_("%s\n (header %s, data %s)\n"), targ->name,
  225. endian_string (targ->header_byteorder),
  226. endian_string (targ->byteorder));
  227. abfd = bfd_openw (param->filename, targ->name);
  228. if (abfd == NULL)
  229. {
  230. bfd_nonfatal (param->filename);
  231. param->error = 1;
  232. }
  233. else if (!bfd_set_format (abfd, bfd_object))
  234. {
  235. if (bfd_get_error () != bfd_error_invalid_operation)
  236. {
  237. bfd_nonfatal (targ->name);
  238. param->error = 1;
  239. }
  240. }
  241. else
  242. {
  243. enum bfd_architecture a;
  244. for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
  245. if (bfd_set_arch_mach (abfd, a, 0))
  246. {
  247. printf (" %s\n", bfd_printable_arch_mach (a, 0));
  248. param->info[param->count - 1].arch[a - bfd_arch_obscure - 1] = 1;
  249. }
  250. }
  251. if (abfd != NULL)
  252. bfd_close_all_done (abfd);
  253. return param->error;
  254. }
  255. static void
  256. display_target_list (struct display_target *arg)
  257. {
  258. arg->filename = make_temp_file (NULL);
  259. arg->error = 0;
  260. arg->count = 0;
  261. arg->alloc = 0;
  262. arg->info = NULL;
  263. bfd_iterate_over_targets (do_display_target, arg);
  264. unlink (arg->filename);
  265. free (arg->filename);
  266. }
  267. /* Calculate how many targets we can print across the page. */
  268. static int
  269. do_info_size (int targ, int width, const struct display_target *arg)
  270. {
  271. while (targ < arg->count)
  272. {
  273. width -= strlen (arg->info[targ].name) + 1;
  274. if (width < 0)
  275. return targ;
  276. ++targ;
  277. }
  278. return targ;
  279. }
  280. /* Print header of target names. */
  281. static void
  282. do_info_header (int targ, int stop_targ, const struct display_target *arg)
  283. {
  284. while (targ != stop_targ)
  285. printf ("%s ", arg->info[targ++].name);
  286. }
  287. /* Print a table row. */
  288. static void
  289. do_info_row (int targ, int stop_targ, enum bfd_architecture a,
  290. const struct display_target *arg)
  291. {
  292. while (targ != stop_targ)
  293. {
  294. if (arg->info[targ].arch[a - bfd_arch_obscure - 1])
  295. fputs (arg->info[targ].name, stdout);
  296. else
  297. {
  298. int l = strlen (arg->info[targ].name);
  299. while (l--)
  300. putchar ('-');
  301. }
  302. ++targ;
  303. if (targ != stop_targ)
  304. putchar (' ');
  305. }
  306. }
  307. /* Print tables of all the target-architecture combinations that
  308. BFD has been configured to support. */
  309. static void
  310. display_target_tables (const struct display_target *arg)
  311. {
  312. const char *columns;
  313. int width, start_targ, stop_targ;
  314. enum bfd_architecture arch;
  315. int longest_arch = 0;
  316. for (arch = bfd_arch_obscure + 1; arch < bfd_arch_last; arch++)
  317. {
  318. const char *s = bfd_printable_arch_mach (arch, 0);
  319. int len = strlen (s);
  320. if (len > longest_arch)
  321. longest_arch = len;
  322. }
  323. width = 0;
  324. columns = getenv ("COLUMNS");
  325. if (columns != NULL)
  326. width = atoi (columns);
  327. if (width == 0)
  328. width = 80;
  329. for (start_targ = 0; start_targ < arg->count; start_targ = stop_targ)
  330. {
  331. stop_targ = do_info_size (start_targ, width - longest_arch - 1, arg);
  332. printf ("\n%*s", longest_arch + 1, " ");
  333. do_info_header (start_targ, stop_targ, arg);
  334. putchar ('\n');
  335. for (arch = bfd_arch_obscure + 1; arch < bfd_arch_last; arch++)
  336. {
  337. if (strcmp (bfd_printable_arch_mach (arch, 0), "UNKNOWN!") != 0)
  338. {
  339. printf ("%*s ", longest_arch,
  340. bfd_printable_arch_mach (arch, 0));
  341. do_info_row (start_targ, stop_targ, arch, arg);
  342. putchar ('\n');
  343. }
  344. }
  345. }
  346. }
  347. int
  348. display_info (void)
  349. {
  350. struct display_target arg;
  351. printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
  352. display_target_list (&arg);
  353. if (!arg.error)
  354. display_target_tables (&arg);
  355. return arg.error;
  356. }
  357. /* Display the archive header for an element as if it were an ls -l listing:
  358. Mode User\tGroup\tSize\tDate Name */
  359. void
  360. print_arelt_descr (FILE *file, bfd *abfd, bool verbose, bool offsets)
  361. {
  362. struct stat buf;
  363. if (verbose)
  364. {
  365. if (bfd_stat_arch_elt (abfd, &buf) == 0)
  366. {
  367. char modebuf[11];
  368. char timebuf[40];
  369. time_t when = buf.st_mtime;
  370. const char *ctime_result = (const char *) ctime (&when);
  371. bfd_size_type size;
  372. /* PR binutils/17605: Check for corrupt time values. */
  373. if (ctime_result == NULL)
  374. sprintf (timebuf, _("<time data corrupt>"));
  375. else
  376. /* POSIX format: skip weekday and seconds from ctime output. */
  377. sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
  378. mode_string (buf.st_mode, modebuf);
  379. modebuf[10] = '\0';
  380. size = buf.st_size;
  381. /* POSIX 1003.2/D11 says to skip first character (entry type). */
  382. fprintf (file, "%s %ld/%ld %6" BFD_VMA_FMT "u %s ", modebuf + 1,
  383. (long) buf.st_uid, (long) buf.st_gid,
  384. size, timebuf);
  385. }
  386. }
  387. fprintf (file, "%s", bfd_get_filename (abfd));
  388. if (offsets)
  389. {
  390. if (bfd_is_thin_archive (abfd) && abfd->proxy_origin)
  391. fprintf (file, " 0x%lx", (unsigned long) abfd->proxy_origin);
  392. else if (!bfd_is_thin_archive (abfd) && abfd->origin)
  393. fprintf (file, " 0x%lx", (unsigned long) abfd->origin);
  394. }
  395. fprintf (file, "\n");
  396. }
  397. /* Return a path for a new temporary file in the same directory
  398. as file PATH. */
  399. static char *
  400. template_in_dir (const char *path)
  401. {
  402. #define template "stXXXXXX"
  403. const char *slash = strrchr (path, '/');
  404. char *tmpname;
  405. size_t len;
  406. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  407. {
  408. /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
  409. char *bslash = strrchr (path, '\\');
  410. if (slash == NULL || (bslash != NULL && bslash > slash))
  411. slash = bslash;
  412. if (slash == NULL && path[0] != '\0' && path[1] == ':')
  413. slash = path + 1;
  414. }
  415. #endif
  416. if (slash != (char *) NULL)
  417. {
  418. len = slash - path;
  419. tmpname = (char *) xmalloc (len + sizeof (template) + 2);
  420. memcpy (tmpname, path, len);
  421. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  422. /* If tmpname is "X:", appending a slash will make it a root
  423. directory on drive X, which is NOT the same as the current
  424. directory on drive X. */
  425. if (len == 2 && tmpname[1] == ':')
  426. tmpname[len++] = '.';
  427. #endif
  428. tmpname[len++] = '/';
  429. }
  430. else
  431. {
  432. tmpname = (char *) xmalloc (sizeof (template));
  433. len = 0;
  434. }
  435. memcpy (tmpname + len, template, sizeof (template));
  436. return tmpname;
  437. #undef template
  438. }
  439. /* Return the name of a created temporary file in the same directory
  440. as FILENAME. */
  441. char *
  442. make_tempname (const char *filename, int *ofd)
  443. {
  444. char *tmpname = template_in_dir (filename);
  445. int fd;
  446. #ifdef HAVE_MKSTEMP
  447. fd = mkstemp (tmpname);
  448. #else
  449. tmpname = mktemp (tmpname);
  450. if (tmpname == NULL)
  451. return NULL;
  452. fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
  453. #endif
  454. if (fd == -1)
  455. {
  456. free (tmpname);
  457. return NULL;
  458. }
  459. *ofd = fd;
  460. return tmpname;
  461. }
  462. /* Return the name of a created temporary directory inside the
  463. directory containing FILENAME. */
  464. char *
  465. make_tempdir (const char *filename)
  466. {
  467. char *tmpname = template_in_dir (filename);
  468. #ifdef HAVE_MKDTEMP
  469. return mkdtemp (tmpname);
  470. #else
  471. tmpname = mktemp (tmpname);
  472. if (tmpname == NULL)
  473. return NULL;
  474. #if defined (_WIN32) && !defined (__CYGWIN32__)
  475. if (mkdir (tmpname) != 0)
  476. return NULL;
  477. #else
  478. if (mkdir (tmpname, 0700) != 0)
  479. return NULL;
  480. #endif
  481. return tmpname;
  482. #endif
  483. }
  484. /* Parse a string into a VMA, with a fatal error if it can't be
  485. parsed. */
  486. bfd_vma
  487. parse_vma (const char *s, const char *arg)
  488. {
  489. bfd_vma ret;
  490. const char *end;
  491. ret = bfd_scan_vma (s, &end, 0);
  492. if (*end != '\0')
  493. fatal (_("%s: bad number: %s"), arg, s);
  494. return ret;
  495. }
  496. /* Returns the size of the named file. If the file does not
  497. exist, or if it is not a real file, then a suitable non-fatal
  498. error message is printed and (off_t) -1 is returned. */
  499. off_t
  500. get_file_size (const char * file_name)
  501. {
  502. struct stat statbuf;
  503. if (file_name == NULL)
  504. return (off_t) -1;
  505. if (stat (file_name, &statbuf) < 0)
  506. {
  507. if (errno == ENOENT)
  508. non_fatal (_("'%s': No such file"), file_name);
  509. else
  510. non_fatal (_("Warning: could not locate '%s'. reason: %s"),
  511. file_name, strerror (errno));
  512. }
  513. else if (S_ISDIR (statbuf.st_mode))
  514. non_fatal (_("Warning: '%s' is a directory"), file_name);
  515. else if (! S_ISREG (statbuf.st_mode))
  516. non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
  517. else if (statbuf.st_size < 0)
  518. non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
  519. file_name);
  520. #if defined (_WIN32) && !defined (__CYGWIN__)
  521. else if (statbuf.st_size == 0)
  522. {
  523. /* MS-Windows 'stat' reports the null device as a regular file;
  524. fix that. */
  525. int fd = open (file_name, O_RDONLY | O_BINARY);
  526. if (isatty (fd))
  527. {
  528. close (fd);
  529. non_fatal (_("Warning: '%s' is not an ordinary file"),
  530. /* libtool wants to see /dev/null in the output. */
  531. strcasecmp (file_name, "nul") ? file_name : "/dev/null");
  532. }
  533. }
  534. #endif
  535. else
  536. return statbuf.st_size;
  537. return (off_t) -1;
  538. }
  539. /* Return the filename in a static buffer. */
  540. const char *
  541. bfd_get_archive_filename (const bfd *abfd)
  542. {
  543. static size_t curr = 0;
  544. static char *buf;
  545. size_t needed;
  546. assert (abfd != NULL);
  547. if (abfd->my_archive == NULL
  548. || bfd_is_thin_archive (abfd->my_archive))
  549. return bfd_get_filename (abfd);
  550. needed = (strlen (bfd_get_filename (abfd->my_archive))
  551. + strlen (bfd_get_filename (abfd)) + 3);
  552. if (needed > curr)
  553. {
  554. if (curr)
  555. free (buf);
  556. curr = needed + (needed >> 1);
  557. buf = (char *) xmalloc (curr);
  558. }
  559. sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
  560. bfd_get_filename (abfd));
  561. return buf;
  562. }
  563. /* Returns TRUE iff PATHNAME, a filename of an archive member,
  564. is valid for writing. For security reasons absolute paths
  565. and paths containing /../ are not allowed. See PR 17533. */
  566. bool
  567. is_valid_archive_path (char const * pathname)
  568. {
  569. const char * n = pathname;
  570. if (IS_ABSOLUTE_PATH (n))
  571. return false;
  572. while (*n)
  573. {
  574. if (*n == '.' && *++n == '.' && ( ! *++n || IS_DIR_SEPARATOR (*n)))
  575. return false;
  576. while (*n && ! IS_DIR_SEPARATOR (*n))
  577. n++;
  578. while (IS_DIR_SEPARATOR (*n))
  579. n++;
  580. }
  581. return true;
  582. }