PageRenderTime 29ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/binutils/bucomm.c

https://github.com/CTSRD-CHERI/binutils
C | 562 lines | 412 code | 84 blank | 66 comment | 94 complexity | c26d9899a849a9e83b65a724eaca10f8 MD5 | raw file
  1. /* bucomm.c -- Bin Utils COMmon code.
  2. Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
  3. 2003, 2006, 2007
  4. Free Software Foundation, Inc.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* We might put this in a library someday so it could be dynamically
  19. loaded, but for now it's not necessary. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libiberty.h"
  23. #include "filenames.h"
  24. #include "libbfd.h"
  25. #include <sys/stat.h>
  26. #include <time.h> /* ctime, maybe time_t */
  27. #include <assert.h>
  28. #include "bucomm.h"
  29. #ifndef HAVE_TIME_T_IN_TIME_H
  30. #ifndef HAVE_TIME_T_IN_TYPES_H
  31. typedef long time_t;
  32. #endif
  33. #endif
  34. static const char * endian_string (enum bfd_endian);
  35. static int display_target_list (void);
  36. static int display_info_table (int, int);
  37. static int display_target_tables (void);
  38. /* Error reporting. */
  39. char *program_name;
  40. void
  41. bfd_nonfatal (const char *string)
  42. {
  43. const char *errmsg = bfd_errmsg (bfd_get_error ());
  44. if (string)
  45. fprintf (stderr, "%s: %s: %s\n", program_name, string, errmsg);
  46. else
  47. fprintf (stderr, "%s: %s\n", program_name, errmsg);
  48. }
  49. void
  50. bfd_fatal (const char *string)
  51. {
  52. bfd_nonfatal (string);
  53. xexit (1);
  54. }
  55. void
  56. report (const char * format, va_list args)
  57. {
  58. fprintf (stderr, "%s: ", program_name);
  59. vfprintf (stderr, format, args);
  60. putc ('\n', stderr);
  61. }
  62. void
  63. fatal VPARAMS ((const char *format, ...))
  64. {
  65. VA_OPEN (args, format);
  66. VA_FIXEDARG (args, const char *, format);
  67. report (format, args);
  68. VA_CLOSE (args);
  69. xexit (1);
  70. }
  71. void
  72. non_fatal VPARAMS ((const char *format, ...))
  73. {
  74. VA_OPEN (args, format);
  75. VA_FIXEDARG (args, const char *, format);
  76. report (format, args);
  77. VA_CLOSE (args);
  78. }
  79. /* Set the default BFD target based on the configured target. Doing
  80. this permits the binutils to be configured for a particular target,
  81. and linked against a shared BFD library which was configured for a
  82. different target. */
  83. void
  84. set_default_bfd_target (void)
  85. {
  86. /* The macro TARGET is defined by Makefile. */
  87. const char *target = TARGET;
  88. if (! bfd_set_default_target (target))
  89. fatal (_("can't set BFD default target to `%s': %s"),
  90. target, bfd_errmsg (bfd_get_error ()));
  91. }
  92. /* After a FALSE return from bfd_check_format_matches with
  93. bfd_get_error () == bfd_error_file_ambiguously_recognized, print
  94. the possible matching targets. */
  95. void
  96. list_matching_formats (char **p)
  97. {
  98. fprintf (stderr, _("%s: Matching formats:"), program_name);
  99. while (*p)
  100. fprintf (stderr, " %s", *p++);
  101. fputc ('\n', stderr);
  102. }
  103. /* List the supported targets. */
  104. void
  105. list_supported_targets (const char *name, FILE *f)
  106. {
  107. int t;
  108. const char **targ_names = bfd_target_list ();
  109. if (name == NULL)
  110. fprintf (f, _("Supported targets:"));
  111. else
  112. fprintf (f, _("%s: supported targets:"), name);
  113. for (t = 0; targ_names[t] != NULL; t++)
  114. fprintf (f, " %s", targ_names[t]);
  115. fprintf (f, "\n");
  116. free (targ_names);
  117. }
  118. /* List the supported architectures. */
  119. void
  120. list_supported_architectures (const char *name, FILE *f)
  121. {
  122. const char **arch;
  123. if (name == NULL)
  124. fprintf (f, _("Supported architectures:"));
  125. else
  126. fprintf (f, _("%s: supported architectures:"), name);
  127. for (arch = bfd_arch_list (); *arch; arch++)
  128. fprintf (f, " %s", *arch);
  129. fprintf (f, "\n");
  130. }
  131. /* The length of the longest architecture name + 1. */
  132. #define LONGEST_ARCH sizeof ("powerpc:common")
  133. static const char *
  134. endian_string (enum bfd_endian endian)
  135. {
  136. switch (endian)
  137. {
  138. case BFD_ENDIAN_BIG: return "big endian";
  139. case BFD_ENDIAN_LITTLE: return "little endian";
  140. default: return "endianness unknown";
  141. }
  142. }
  143. /* List the targets that BFD is configured to support, each followed
  144. by its endianness and the architectures it supports. */
  145. static int
  146. display_target_list (void)
  147. {
  148. char *dummy_name;
  149. int t;
  150. int ret = 1;
  151. dummy_name = make_temp_file (NULL);
  152. for (t = 0; bfd_target_vector[t]; t++)
  153. {
  154. const bfd_target *p = bfd_target_vector[t];
  155. bfd *abfd = bfd_openw (dummy_name, p->name);
  156. enum bfd_architecture a;
  157. printf ("%s\n (header %s, data %s)\n", p->name,
  158. endian_string (p->header_byteorder),
  159. endian_string (p->byteorder));
  160. if (abfd == NULL)
  161. {
  162. bfd_nonfatal (dummy_name);
  163. ret = 0;
  164. continue;
  165. }
  166. if (! bfd_set_format (abfd, bfd_object))
  167. {
  168. if (bfd_get_error () != bfd_error_invalid_operation)
  169. {
  170. bfd_nonfatal (p->name);
  171. ret = 0;
  172. }
  173. bfd_close_all_done (abfd);
  174. continue;
  175. }
  176. for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
  177. if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
  178. printf (" %s\n",
  179. bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
  180. bfd_close_all_done (abfd);
  181. }
  182. unlink (dummy_name);
  183. free (dummy_name);
  184. return ret;
  185. }
  186. /* Print a table showing which architectures are supported for entries
  187. FIRST through LAST-1 of bfd_target_vector (targets across,
  188. architectures down). */
  189. static int
  190. display_info_table (int first, int last)
  191. {
  192. int t;
  193. int ret = 1;
  194. char *dummy_name;
  195. enum bfd_architecture a;
  196. /* Print heading of target names. */
  197. printf ("\n%*s", (int) LONGEST_ARCH, " ");
  198. for (t = first; t < last && bfd_target_vector[t]; t++)
  199. printf ("%s ", bfd_target_vector[t]->name);
  200. putchar ('\n');
  201. dummy_name = make_temp_file (NULL);
  202. for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
  203. if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
  204. {
  205. printf ("%*s ", (int) LONGEST_ARCH - 1,
  206. bfd_printable_arch_mach (a, 0));
  207. for (t = first; t < last && bfd_target_vector[t]; t++)
  208. {
  209. const bfd_target *p = bfd_target_vector[t];
  210. bfd_boolean ok = TRUE;
  211. bfd *abfd = bfd_openw (dummy_name, p->name);
  212. if (abfd == NULL)
  213. {
  214. bfd_nonfatal (p->name);
  215. ret = 0;
  216. ok = FALSE;
  217. }
  218. if (ok)
  219. {
  220. if (! bfd_set_format (abfd, bfd_object))
  221. {
  222. if (bfd_get_error () != bfd_error_invalid_operation)
  223. {
  224. bfd_nonfatal (p->name);
  225. ret = 0;
  226. }
  227. ok = FALSE;
  228. }
  229. }
  230. if (ok)
  231. {
  232. if (! bfd_set_arch_mach (abfd, a, 0))
  233. ok = FALSE;
  234. }
  235. if (ok)
  236. printf ("%s ", p->name);
  237. else
  238. {
  239. int l = strlen (p->name);
  240. while (l--)
  241. putchar ('-');
  242. putchar (' ');
  243. }
  244. if (abfd != NULL)
  245. bfd_close_all_done (abfd);
  246. }
  247. putchar ('\n');
  248. }
  249. unlink (dummy_name);
  250. free (dummy_name);
  251. return ret;
  252. }
  253. /* Print tables of all the target-architecture combinations that
  254. BFD has been configured to support. */
  255. static int
  256. display_target_tables (void)
  257. {
  258. int t;
  259. int columns;
  260. int ret = 1;
  261. char *colum;
  262. columns = 0;
  263. colum = getenv ("COLUMNS");
  264. if (colum != NULL)
  265. columns = atoi (colum);
  266. if (columns == 0)
  267. columns = 80;
  268. t = 0;
  269. while (bfd_target_vector[t] != NULL)
  270. {
  271. int oldt = t, wid;
  272. wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
  273. ++t;
  274. while (wid < columns && bfd_target_vector[t] != NULL)
  275. {
  276. int newwid;
  277. newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
  278. if (newwid >= columns)
  279. break;
  280. wid = newwid;
  281. ++t;
  282. }
  283. if (! display_info_table (oldt, t))
  284. ret = 0;
  285. }
  286. return ret;
  287. }
  288. int
  289. display_info (void)
  290. {
  291. printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
  292. if (! display_target_list () || ! display_target_tables ())
  293. return 1;
  294. else
  295. return 0;
  296. }
  297. /* Display the archive header for an element as if it were an ls -l listing:
  298. Mode User\tGroup\tSize\tDate Name */
  299. void
  300. print_arelt_descr (FILE *file, bfd *abfd, bfd_boolean verbose)
  301. {
  302. struct stat buf;
  303. if (verbose)
  304. {
  305. if (bfd_stat_arch_elt (abfd, &buf) == 0)
  306. {
  307. char modebuf[11];
  308. char timebuf[40];
  309. time_t when = buf.st_mtime;
  310. const char *ctime_result = (const char *) ctime (&when);
  311. /* POSIX format: skip weekday and seconds from ctime output. */
  312. sprintf (timebuf, "%.12s %.4s", ctime_result + 4, ctime_result + 20);
  313. mode_string (buf.st_mode, modebuf);
  314. modebuf[10] = '\0';
  315. /* POSIX 1003.2/D11 says to skip first character (entry type). */
  316. fprintf (file, "%s %ld/%ld %6ld %s ", modebuf + 1,
  317. (long) buf.st_uid, (long) buf.st_gid,
  318. (long) buf.st_size, timebuf);
  319. }
  320. }
  321. fprintf (file, "%s\n", bfd_get_filename (abfd));
  322. }
  323. /* Return a path for a new temporary file in the same directory
  324. as file PATH. */
  325. static char *
  326. template_in_dir (const char *path)
  327. {
  328. #define template "stXXXXXX"
  329. const char *slash = strrchr (path, '/');
  330. char *tmpname;
  331. size_t len;
  332. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  333. {
  334. /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
  335. char *bslash = strrchr (path, '\\');
  336. if (slash == NULL || (bslash != NULL && bslash > slash))
  337. slash = bslash;
  338. if (slash == NULL && path[0] != '\0' && path[1] == ':')
  339. slash = path + 1;
  340. }
  341. #endif
  342. if (slash != (char *) NULL)
  343. {
  344. len = slash - path;
  345. tmpname = xmalloc (len + sizeof (template) + 2);
  346. memcpy (tmpname, path, len);
  347. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  348. /* If tmpname is "X:", appending a slash will make it a root
  349. directory on drive X, which is NOT the same as the current
  350. directory on drive X. */
  351. if (len == 2 && tmpname[1] == ':')
  352. tmpname[len++] = '.';
  353. #endif
  354. tmpname[len++] = '/';
  355. }
  356. else
  357. {
  358. tmpname = xmalloc (sizeof (template));
  359. len = 0;
  360. }
  361. memcpy (tmpname + len, template, sizeof (template));
  362. return tmpname;
  363. #undef template
  364. }
  365. /* Return the name of a created temporary file in the same directory
  366. as FILENAME. */
  367. char *
  368. make_tempname (char *filename)
  369. {
  370. char *tmpname = template_in_dir (filename);
  371. int fd;
  372. #ifdef HAVE_MKSTEMP
  373. fd = mkstemp (tmpname);
  374. #else
  375. tmpname = mktemp (tmpname);
  376. if (tmpname == NULL)
  377. return NULL;
  378. fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
  379. #endif
  380. if (fd == -1)
  381. return NULL;
  382. close (fd);
  383. return tmpname;
  384. }
  385. /* Return the name of a created temporary directory inside the
  386. directory containing FILENAME. */
  387. char *
  388. make_tempdir (char *filename)
  389. {
  390. char *tmpname = template_in_dir (filename);
  391. #ifdef HAVE_MKDTEMP
  392. return mkdtemp (tmpname);
  393. #else
  394. tmpname = mktemp (tmpname);
  395. if (tmpname == NULL)
  396. return NULL;
  397. #if defined (_WIN32) && !defined (__CYGWIN32__)
  398. if (mkdir (tmpname) != 0)
  399. return NULL;
  400. #else
  401. if (mkdir (tmpname, 0700) != 0)
  402. return NULL;
  403. #endif
  404. return tmpname;
  405. #endif
  406. }
  407. /* Parse a string into a VMA, with a fatal error if it can't be
  408. parsed. */
  409. bfd_vma
  410. parse_vma (const char *s, const char *arg)
  411. {
  412. bfd_vma ret;
  413. const char *end;
  414. ret = bfd_scan_vma (s, &end, 0);
  415. if (*end != '\0')
  416. fatal (_("%s: bad number: %s"), arg, s);
  417. return ret;
  418. }
  419. /* Returns the size of the named file. If the file does not
  420. exist, or if it is not a real file, then a suitable non-fatal
  421. error message is printed and zero is returned. */
  422. off_t
  423. get_file_size (const char * file_name)
  424. {
  425. struct stat statbuf;
  426. if (stat (file_name, &statbuf) < 0)
  427. {
  428. if (errno == ENOENT)
  429. non_fatal (_("'%s': No such file"), file_name);
  430. else
  431. non_fatal (_("Warning: could not locate '%s'. reason: %s"),
  432. file_name, strerror (errno));
  433. }
  434. else if (! S_ISREG (statbuf.st_mode))
  435. non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
  436. else
  437. return statbuf.st_size;
  438. return 0;
  439. }
  440. /* Return the filename in a static buffer. */
  441. const char *
  442. bfd_get_archive_filename (bfd *abfd)
  443. {
  444. static size_t curr = 0;
  445. static char *buf;
  446. size_t needed;
  447. assert (abfd != NULL);
  448. if (!abfd->my_archive)
  449. return bfd_get_filename (abfd);
  450. needed = (strlen (bfd_get_filename (abfd->my_archive))
  451. + strlen (bfd_get_filename (abfd)) + 3);
  452. if (needed > curr)
  453. {
  454. if (curr)
  455. free (buf);
  456. curr = needed + (needed >> 1);
  457. buf = bfd_malloc (curr);
  458. /* If we can't malloc, fail safe by returning just the file name.
  459. This function is only used when building error messages. */
  460. if (!buf)
  461. {
  462. curr = 0;
  463. return bfd_get_filename (abfd);
  464. }
  465. }
  466. sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
  467. bfd_get_filename (abfd));
  468. return buf;
  469. }