H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 2 | * |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 4 | * 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 17 | * |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 18 | * 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 Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 35 | * error.c - error message handling routines for the assembler |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include "compiler.h" |
| 39 | |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 43 | #include "error.h" |
| 44 | |
| 45 | /* |
| 46 | * Description of the suppressible warnings for the command line and |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 47 | * the [warning] directive. |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 48 | */ |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 49 | const struct warning warnings[ERR_WARN_ALL+1] = { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 50 | { "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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 73 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 74 | /* THIS ENTRY MUST COME LAST */ |
| 75 | { "all", "all possible warnings", false } |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 76 | }; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 77 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 78 | /* Current state and command-line state, for reset */ |
| 79 | uint8_t warning_state[ERR_WARN_ALL]; |
| 80 | uint8_t warning_state_init[ERR_WARN_ALL]; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 81 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 82 | /* Global error handling function */ |
| 83 | vefunc nasm_verror; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 84 | |
| 85 | void nasm_error(int severity, const char *fmt, ...) |
| 86 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 87 | va_list ap; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 88 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 89 | va_start(ap, fmt); |
| 90 | nasm_verror(severity, fmt, ap); |
| 91 | va_end(ap); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 94 | fatal_func nasm_fatal(const char *fmt, ...) |
| 95 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 96 | va_list ap; |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 97 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 98 | va_start(ap, fmt); |
| 99 | nasm_verror(ERR_FATAL, fmt, ap); |
| 100 | abort(); |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | fatal_func nasm_fatal_fl(int flags, const char *fmt, ...) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 104 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 105 | va_list ap; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 106 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 107 | va_start(ap, fmt); |
| 108 | nasm_verror(flags | ERR_FATAL, fmt, ap); |
| 109 | abort(); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 110 | } |
| 111 | |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 112 | fatal_func nasm_panic(const char *fmt, ...) |
| 113 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 114 | va_list ap; |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 115 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 116 | va_start(ap, fmt); |
| 117 | nasm_verror(ERR_PANIC, fmt, ap); |
| 118 | abort(); |
H. Peter Anvin | c513690 | 2018-06-15 18:20:17 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | fatal_func nasm_panic_fl(int flags, const char *fmt, ...) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 122 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 123 | va_list ap; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 124 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 125 | va_start(ap, fmt); |
| 126 | nasm_verror(flags | ERR_PANIC, fmt, ap); |
| 127 | abort(); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 128 | } |
| 129 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 130 | fatal_func nasm_panic_from_macro(const char *file, int line) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 131 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 132 | nasm_panic("internal error at %s:%d\n", file, line); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 135 | fatal_func nasm_assert_failed(const char *file, int line, const char *msg) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 136 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 137 | nasm_panic("assertion %s failed at %s:%d", msg, file, line); |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * This is called when processing a -w or -W option, or a warning directive. |
| 142 | * Returns true if if the action was successful. |
| 143 | */ |
| 144 | bool set_warning_status(const char *value) |
| 145 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 146 | enum warn_action { WID_OFF, WID_ON, WID_RESET }; |
| 147 | enum warn_action action; |
| 148 | bool ok = false; |
| 149 | uint8_t mask; |
| 150 | int i; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 151 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 152 | value = nasm_skip_spaces(value); |
| 153 | switch (*value) { |
| 154 | case '-': |
| 155 | action = WID_OFF; |
| 156 | value++; |
| 157 | break; |
| 158 | case '+': |
| 159 | action = WID_ON; |
| 160 | value++; |
| 161 | break; |
| 162 | case '*': |
| 163 | action = WID_RESET; |
| 164 | value++; |
| 165 | break; |
| 166 | case 'N': |
| 167 | case 'n': |
| 168 | if (!nasm_strnicmp(value, "no-", 3)) { |
| 169 | action = WID_OFF; |
| 170 | value += 3; |
| 171 | break; |
| 172 | } else if (!nasm_stricmp(value, "none")) { |
| 173 | action = WID_OFF; |
| 174 | value = NULL; |
| 175 | break; |
| 176 | } |
| 177 | /* else fall through */ |
| 178 | default: |
| 179 | action = WID_ON; |
| 180 | break; |
| 181 | } |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 182 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 183 | mask = WARN_ST_ENABLED; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 184 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 185 | if (value && !nasm_strnicmp(value, "error", 5)) { |
| 186 | switch (value[5]) { |
| 187 | case '=': |
| 188 | mask = WARN_ST_ERROR; |
| 189 | value += 6; |
| 190 | break; |
| 191 | case '\0': |
| 192 | mask = WARN_ST_ERROR; |
| 193 | value = NULL; |
| 194 | break; |
| 195 | default: |
| 196 | /* Just an accidental prefix? */ |
| 197 | break; |
| 198 | } |
| 199 | } |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 200 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 201 | if (value && !nasm_stricmp(value, "all")) |
| 202 | value = NULL; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 203 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 204 | /* This is inefficient, but it shouldn't matter... */ |
| 205 | for (i = 0; i < ERR_WARN_ALL; i++) { |
| 206 | if (!value || !nasm_stricmp(value, warnings[i].name)) { |
| 207 | ok = true; /* At least one action taken */ |
| 208 | switch (action) { |
| 209 | case WID_OFF: |
| 210 | warning_state[i] &= ~mask; |
| 211 | break; |
| 212 | case WID_ON: |
| 213 | warning_state[i] |= mask; |
| 214 | break; |
| 215 | case WID_RESET: |
| 216 | warning_state[i] &= ~mask; |
| 217 | warning_state[i] |= warning_state_init[i] & mask; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 222 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame^] | 223 | return ok; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 224 | } |