blob: f99baaaf7b0acf02ac265f3a4ff30f14827da993 [file] [log] [blame]
H. Peter Anvin22538e22016-05-25 05:42:47 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08002 *
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin22538e22016-05-25 05:42:47 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080017 *
H. Peter Anvin22538e22016-05-25 05:42:47 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
H. Peter Anvinb20bc732017-03-07 19:23:03 -080035 * error.c - error message handling routines for the assembler
H. Peter Anvin22538e22016-05-25 05:42:47 -070036 */
37
38#include "compiler.h"
39
40#include <stdlib.h>
41
42#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080043#include "error.h"
44
45/*
46 * Description of the suppressible warnings for the command line and
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080047 * the [warning] directive.
H. Peter Anvinb20bc732017-03-07 19:23:03 -080048 */
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080049const struct warning warnings[ERR_WARN_ALL+1] = {
Cyrill Gorcunov33510722018-11-24 18:58:11 +030050 { "other", "any warning not specifially mentioned below", true },
51 { "macro-params", "macro calls with wrong parameter count", true },
52 { "macro-selfref", "cyclic macro references", false },
53 { "macro-defaults", "macros with more default than optional parameters", true },
54 { "orphan-labels", "labels alone on lines without trailing `:'", true },
55 { "number-overflow", "numeric constant does not fit", true },
56 { "gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false },
57 { "float-overflow", "floating point overflow", true },
58 { "float-denorm", "floating point denormal", false },
59 { "float-underflow", "floating point underflow", false },
60 { "float-toolong", "too many digits in floating-point number", true },
61 { "user", "%warning directives", true },
62 { "lock", "lock prefix on unlockable instructions", true },
63 { "hle", "invalid hle prefixes", true },
64 { "bnd", "invalid bnd prefixes", true },
65 { "zext-reloc", "relocation zero-extended to match output format", true },
66 { "ptr", "non-NASM keyword used in other assemblers", true },
67 { "bad-pragma", "empty or malformed %pragma", false },
68 { "unknown-pragma", "unknown %pragma facility or directive", false },
69 { "not-my-pragma", "%pragma not applicable to this compilation", false },
70 { "unknown-warning", "unknown warning in -W/-w or warning directive", false },
71 { "negative-rep", "regative %rep count", true },
72 { "phase", "phase error during stabilization", false },
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080073
Cyrill Gorcunov33510722018-11-24 18:58:11 +030074 /* THIS ENTRY MUST COME LAST */
75 { "all", "all possible warnings", false }
H. Peter Anvinb20bc732017-03-07 19:23:03 -080076};
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080077
Cyrill Gorcunov33510722018-11-24 18:58:11 +030078/* Current state and command-line state, for reset */
79uint8_t warning_state[ERR_WARN_ALL];
80uint8_t warning_state_init[ERR_WARN_ALL];
H. Peter Anvin22538e22016-05-25 05:42:47 -070081
Cyrill Gorcunov33510722018-11-24 18:58:11 +030082/* Global error handling function */
83vefunc nasm_verror;
H. Peter Anvin22538e22016-05-25 05:42:47 -070084
85void nasm_error(int severity, const char *fmt, ...)
86{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030087 va_list ap;
H. Peter Anvin22538e22016-05-25 05:42:47 -070088
Cyrill Gorcunov33510722018-11-24 18:58:11 +030089 va_start(ap, fmt);
90 nasm_verror(severity, fmt, ap);
91 va_end(ap);
H. Peter Anvin22538e22016-05-25 05:42:47 -070092}
93
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030094#define nasm_error_generatorf(__sev, __flags, __fmt) \
95 va_list __ap; \
96 va_start(__ap, __fmt); \
97 nasm_verror(__sev | __flags, __fmt, __ap)
98
99#define nasm_error_generator(__sev, __fmt) \
100 nasm_error_generatorf(__sev, 0, __fmt)
101
102void nasm_debug(const char *fmt, ...)
103{
104 nasm_error_generator(ERR_DEBUG, fmt);
105}
106
107void nasm_debugf(int flags, const char *fmt, ...)
108{
109 nasm_error_generatorf(ERR_DEBUG, flags, fmt);
110}
111
112void nasm_warn(const char *fmt, ...)
113{
114 nasm_error_generator(ERR_WARNING, fmt);
115}
116
117void nasm_warnf(int flags, const char *fmt, ...)
118{
119 nasm_error_generatorf(ERR_WARNING, flags, fmt);
120}
121
122void nasm_nonfatal(const char *fmt, ...)
123{
124 nasm_error_generator(ERR_NONFATAL, fmt);
125}
126
127void nasm_nonfatalf(int flags, const char *fmt, ...)
128{
129 nasm_error_generatorf(ERR_NONFATAL, flags, fmt);
130}
131
H. Peter Anvinc5136902018-06-15 18:20:17 -0700132fatal_func nasm_fatal(const char *fmt, ...)
133{
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300134 nasm_error_generator(ERR_FATAL, fmt);
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300135 abort();
H. Peter Anvinc5136902018-06-15 18:20:17 -0700136}
137
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300138fatal_func nasm_fatalf(int flags, const char *fmt, ...)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700139{
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300140 nasm_error_generatorf(ERR_FATAL, flags, fmt);
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300141 abort();
H. Peter Anvin22538e22016-05-25 05:42:47 -0700142}
143
H. Peter Anvinc5136902018-06-15 18:20:17 -0700144fatal_func nasm_panic(const char *fmt, ...)
145{
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300146 nasm_error_generator(ERR_PANIC, fmt);
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300147 abort();
H. Peter Anvinc5136902018-06-15 18:20:17 -0700148}
149
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300150fatal_func nasm_panicf(int flags, const char *fmt, ...)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700151{
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300152 nasm_error_generatorf(ERR_PANIC, flags, fmt);
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300153 abort();
H. Peter Anvin22538e22016-05-25 05:42:47 -0700154}
155
H. Peter Anvin6686fc62018-02-22 14:52:50 -0800156fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700157{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300158 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -0700159}
160
H. Peter Anvin6686fc62018-02-22 14:52:50 -0800161fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700162{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300163 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800164}
165
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300166#undef nasm_error_generator
167#undef nasm_error_generatorf
168
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800169/*
170 * This is called when processing a -w or -W option, or a warning directive.
171 * Returns true if if the action was successful.
172 */
173bool set_warning_status(const char *value)
174{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300175 enum warn_action { WID_OFF, WID_ON, WID_RESET };
176 enum warn_action action;
177 bool ok = false;
178 uint8_t mask;
179 int i;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800180
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300181 value = nasm_skip_spaces(value);
182 switch (*value) {
183 case '-':
184 action = WID_OFF;
185 value++;
186 break;
187 case '+':
188 action = WID_ON;
189 value++;
190 break;
191 case '*':
192 action = WID_RESET;
193 value++;
194 break;
195 case 'N':
196 case 'n':
197 if (!nasm_strnicmp(value, "no-", 3)) {
198 action = WID_OFF;
199 value += 3;
200 break;
201 } else if (!nasm_stricmp(value, "none")) {
202 action = WID_OFF;
203 value = NULL;
204 break;
205 }
206 /* else fall through */
207 default:
208 action = WID_ON;
209 break;
210 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800211
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300212 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800213
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300214 if (value && !nasm_strnicmp(value, "error", 5)) {
215 switch (value[5]) {
216 case '=':
217 mask = WARN_ST_ERROR;
218 value += 6;
219 break;
220 case '\0':
221 mask = WARN_ST_ERROR;
222 value = NULL;
223 break;
224 default:
225 /* Just an accidental prefix? */
226 break;
227 }
228 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800229
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300230 if (value && !nasm_stricmp(value, "all"))
231 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800232
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300233 /* This is inefficient, but it shouldn't matter... */
234 for (i = 0; i < ERR_WARN_ALL; i++) {
235 if (!value || !nasm_stricmp(value, warnings[i].name)) {
236 ok = true; /* At least one action taken */
237 switch (action) {
238 case WID_OFF:
239 warning_state[i] &= ~mask;
240 break;
241 case WID_ON:
242 warning_state[i] |= mask;
243 break;
244 case WID_RESET:
245 warning_state[i] &= ~mask;
246 warning_state[i] |= warning_state_init[i] & mask;
247 break;
248 }
249 }
250 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800251
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300252 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700253}