messages.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* messages.c - error reporter -
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS 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, or (at your option)
  7. any later version.
  8. GAS 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 GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. static void identify (char *);
  18. static void as_show_where (void);
  19. static void as_warn_internal (char *, unsigned int, char *);
  20. static void as_bad_internal (char *, unsigned int, char *);
  21. /* Despite the rest of the comments in this file, (FIXME-SOON),
  22. here is the current scheme for error messages etc:
  23. as_fatal() is used when gas is quite confused and
  24. continuing the assembly is pointless. In this case we
  25. exit immediately with error status.
  26. as_bad() is used to mark errors that result in what we
  27. presume to be a useless object file. Say, we ignored
  28. something that might have been vital. If we see any of
  29. these, assembly will continue to the end of the source,
  30. no object file will be produced, and we will terminate
  31. with error status. The new option, -Z, tells us to
  32. produce an object file anyway but we still exit with
  33. error status. The assumption here is that you don't want
  34. this object file but we could be wrong.
  35. as_warn() is used when we have an error from which we
  36. have a plausible error recovery. eg, masking the top
  37. bits of a constant that is longer than will fit in the
  38. destination. In this case we will continue to assemble
  39. the source, although we may have made a bad assumption,
  40. and we will produce an object file and return normal exit
  41. status (ie, no error). The new option -X tells us to
  42. treat all as_warn() errors as as_bad() errors. That is,
  43. no object file will be produced and we will exit with
  44. error status. The idea here is that we don't kill an
  45. entire make because of an error that we knew how to
  46. correct. On the other hand, sometimes you might want to
  47. stop the make at these points.
  48. as_tsktsk() is used when we see a minor error for which
  49. our error recovery action is almost certainly correct.
  50. In this case, we print a message and then assembly
  51. continues as though no error occurred. */
  52. static void
  53. identify (char *file)
  54. {
  55. static int identified;
  56. if (identified)
  57. return;
  58. identified++;
  59. if (!file)
  60. {
  61. unsigned int x;
  62. as_where (&file, &x);
  63. }
  64. if (file)
  65. fprintf (stderr, "%s: ", file);
  66. fprintf (stderr, _("Assembler messages:\n"));
  67. }
  68. /* The number of warnings issued. */
  69. static int warning_count;
  70. int
  71. had_warnings (void)
  72. {
  73. return warning_count;
  74. }
  75. /* Nonzero if we've hit a 'bad error', and should not write an obj file,
  76. and exit with a nonzero error code. */
  77. static int error_count;
  78. int
  79. had_errors (void)
  80. {
  81. return error_count;
  82. }
  83. /* Print the current location to stderr. */
  84. static void
  85. as_show_where (void)
  86. {
  87. char *file;
  88. unsigned int line;
  89. as_where (&file, &line);
  90. identify (file);
  91. if (file)
  92. {
  93. if (line != 0)
  94. fprintf (stderr, "%s:%u: ", file, line);
  95. else
  96. fprintf (stderr, "%s: ", file);
  97. }
  98. }
  99. /* Send to stderr a string as a warning, and locate warning
  100. in input file(s).
  101. Please only use this for when we have some recovery action.
  102. Please explain in string (which may have '\n's) what recovery was
  103. done. */
  104. void
  105. as_tsktsk (const char *format, ...)
  106. {
  107. va_list args;
  108. as_show_where ();
  109. va_start (args, format);
  110. vfprintf (stderr, format, args);
  111. va_end (args);
  112. (void) putc ('\n', stderr);
  113. }
  114. /* The common portion of as_warn and as_warn_where. */
  115. static void
  116. as_warn_internal (char *file, unsigned int line, char *buffer)
  117. {
  118. ++warning_count;
  119. if (file == NULL)
  120. as_where (&file, &line);
  121. identify (file);
  122. if (file)
  123. {
  124. if (line != 0)
  125. fprintf (stderr, "%s:%u: %s%s\n", file, line, _("Warning: "), buffer);
  126. else
  127. fprintf (stderr, "%s: %s%s\n", file, _("Warning: "), buffer);
  128. }
  129. else
  130. fprintf (stderr, "%s%s\n", _("Warning: "), buffer);
  131. #ifndef NO_LISTING
  132. listing_warning (buffer);
  133. #endif
  134. }
  135. /* Send to stderr a string as a warning, and locate warning
  136. in input file(s).
  137. Please only use this for when we have some recovery action.
  138. Please explain in string (which may have '\n's) what recovery was
  139. done. */
  140. void
  141. as_warn (const char *format, ...)
  142. {
  143. va_list args;
  144. char buffer[2000];
  145. if (!flag_no_warnings)
  146. {
  147. va_start (args, format);
  148. vsnprintf (buffer, sizeof (buffer), format, args);
  149. va_end (args);
  150. as_warn_internal ((char *) NULL, 0, buffer);
  151. }
  152. }
  153. /* Like as_bad but the file name and line number are passed in.
  154. Unfortunately, we have to repeat the function in order to handle
  155. the varargs correctly and portably. */
  156. void
  157. as_warn_where (char *file, unsigned int line, const char *format, ...)
  158. {
  159. va_list args;
  160. char buffer[2000];
  161. if (!flag_no_warnings)
  162. {
  163. va_start (args, format);
  164. vsnprintf (buffer, sizeof (buffer), format, args);
  165. va_end (args);
  166. as_warn_internal (file, line, buffer);
  167. }
  168. }
  169. /* The common portion of as_bad and as_bad_where. */
  170. static void
  171. as_bad_internal (char *file, unsigned int line, char *buffer)
  172. {
  173. ++error_count;
  174. if (file == NULL)
  175. as_where (&file, &line);
  176. identify (file);
  177. if (file)
  178. {
  179. if (line != 0)
  180. fprintf (stderr, "%s:%u: %s%s\n", file, line, _("Error: "), buffer);
  181. else
  182. fprintf (stderr, "%s: %s%s\n", file, _("Error: "), buffer);
  183. }
  184. else
  185. fprintf (stderr, "%s%s\n", _("Error: "), buffer);
  186. #ifndef NO_LISTING
  187. listing_error (buffer);
  188. #endif
  189. }
  190. /* Send to stderr a string as a warning, and locate warning in input
  191. file(s). Please us when there is no recovery, but we want to
  192. continue processing but not produce an object file.
  193. Please explain in string (which may have '\n's) what recovery was
  194. done. */
  195. void
  196. as_bad (const char *format, ...)
  197. {
  198. va_list args;
  199. char buffer[2000];
  200. va_start (args, format);
  201. vsnprintf (buffer, sizeof (buffer), format, args);
  202. va_end (args);
  203. as_bad_internal ((char *) NULL, 0, buffer);
  204. }
  205. /* Like as_bad but the file name and line number are passed in.
  206. Unfortunately, we have to repeat the function in order to handle
  207. the varargs correctly and portably. */
  208. void
  209. as_bad_where (char *file, unsigned int line, const char *format, ...)
  210. {
  211. va_list args;
  212. char buffer[2000];
  213. va_start (args, format);
  214. vsnprintf (buffer, sizeof (buffer), format, args);
  215. va_end (args);
  216. as_bad_internal (file, line, buffer);
  217. }
  218. /* Send to stderr a string as a fatal message, and print location of
  219. error in input file(s).
  220. Please only use this for when we DON'T have some recovery action.
  221. It xexit()s with a warning status. */
  222. void
  223. as_fatal (const char *format, ...)
  224. {
  225. va_list args;
  226. as_show_where ();
  227. va_start (args, format);
  228. fprintf (stderr, _("Fatal error: "));
  229. vfprintf (stderr, format, args);
  230. (void) putc ('\n', stderr);
  231. va_end (args);
  232. /* Delete the output file, if it exists. This will prevent make from
  233. thinking that a file was created and hence does not need rebuilding. */
  234. if (out_file_name != NULL)
  235. unlink_if_ordinary (out_file_name);
  236. xexit (EXIT_FAILURE);
  237. }
  238. /* Indicate assertion failure.
  239. Arguments: Filename, line number, optional function name. */
  240. void
  241. as_assert (const char *file, int line, const char *fn)
  242. {
  243. as_show_where ();
  244. fprintf (stderr, _("Internal error!\n"));
  245. if (fn)
  246. fprintf (stderr, _("Assertion failure in %s at %s:%d.\n"),
  247. fn, file, line);
  248. else
  249. fprintf (stderr, _("Assertion failure at %s:%d.\n"), file, line);
  250. fprintf (stderr, _("Please report this bug.\n"));
  251. xexit (EXIT_FAILURE);
  252. }
  253. /* as_abort: Print a friendly message saying how totally hosed we are,
  254. and exit without producing a core file. */
  255. void
  256. as_abort (const char *file, int line, const char *fn)
  257. {
  258. as_show_where ();
  259. if (fn)
  260. fprintf (stderr, _("Internal error, aborting at %s:%d in %s\n"),
  261. file, line, fn);
  262. else
  263. fprintf (stderr, _("Internal error, aborting at %s:%d\n"),
  264. file, line);
  265. fprintf (stderr, _("Please report this bug.\n"));
  266. xexit (EXIT_FAILURE);
  267. }
  268. /* Support routines. */
  269. void
  270. sprint_value (char *buf, valueT val)
  271. {
  272. if (sizeof (val) <= sizeof (long))
  273. {
  274. sprintf (buf, "%ld", (long) val);
  275. return;
  276. }
  277. if (sizeof (val) <= sizeof (bfd_vma))
  278. {
  279. sprintf_vma (buf, val);
  280. return;
  281. }
  282. abort ();
  283. }
  284. #define HEX_MAX_THRESHOLD 1024
  285. #define HEX_MIN_THRESHOLD -(HEX_MAX_THRESHOLD)
  286. static void
  287. as_internal_value_out_of_range (char * prefix,
  288. offsetT val,
  289. offsetT min,
  290. offsetT max,
  291. char * file,
  292. unsigned line,
  293. int bad)
  294. {
  295. const char * err;
  296. if (prefix == NULL)
  297. prefix = "";
  298. if (val >= min && val <= max)
  299. {
  300. addressT right = max & -max;
  301. if (max <= 1)
  302. abort ();
  303. /* xgettext:c-format */
  304. err = _("%s out of domain (%d is not a multiple of %d)");
  305. if (bad)
  306. as_bad_where (file, line, err,
  307. prefix, (int) val, (int) right);
  308. else
  309. as_warn_where (file, line, err,
  310. prefix, (int) val, (int) right);
  311. return;
  312. }
  313. if ( val < HEX_MAX_THRESHOLD
  314. && min < HEX_MAX_THRESHOLD
  315. && max < HEX_MAX_THRESHOLD
  316. && val > HEX_MIN_THRESHOLD
  317. && min > HEX_MIN_THRESHOLD
  318. && max > HEX_MIN_THRESHOLD)
  319. {
  320. /* xgettext:c-format */
  321. err = _("%s out of range (%d is not between %d and %d)");
  322. if (bad)
  323. as_bad_where (file, line, err,
  324. prefix, (int) val, (int) min, (int) max);
  325. else
  326. as_warn_where (file, line, err,
  327. prefix, (int) val, (int) min, (int) max);
  328. }
  329. else
  330. {
  331. char val_buf [sizeof (val) * 3 + 2];
  332. char min_buf [sizeof (val) * 3 + 2];
  333. char max_buf [sizeof (val) * 3 + 2];
  334. if (sizeof (val) > sizeof (bfd_vma))
  335. abort ();
  336. sprintf_vma (val_buf, (bfd_vma) val);
  337. sprintf_vma (min_buf, (bfd_vma) min);
  338. sprintf_vma (max_buf, (bfd_vma) max);
  339. /* xgettext:c-format. */
  340. err = _("%s out of range (0x%s is not between 0x%s and 0x%s)");
  341. if (bad)
  342. as_bad_where (file, line, err, prefix, val_buf, min_buf, max_buf);
  343. else
  344. as_warn_where (file, line, err, prefix, val_buf, min_buf, max_buf);
  345. }
  346. }
  347. void
  348. as_warn_value_out_of_range (char * prefix,
  349. offsetT value,
  350. offsetT min,
  351. offsetT max,
  352. char * file,
  353. unsigned line)
  354. {
  355. as_internal_value_out_of_range (prefix, value, min, max, file, line, 0);
  356. }
  357. void
  358. as_bad_value_out_of_range (char * prefix,
  359. offsetT value,
  360. offsetT min,
  361. offsetT max,
  362. char * file,
  363. unsigned line)
  364. {
  365. as_internal_value_out_of_range (prefix, value, min, max, file, line, 1);
  366. }