bucomm.c 15 KB

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