0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-11-08 23:27:15 -05:00
Commit Graph

689 Commits

Author SHA1 Message Date
redzimski
94c6ecda5b fix [warning push] causing "unknown warning name"
[warning push] emitted a "unknown warning name" warning when
-w+unknown-warning is set.
2025-11-04 10:23:54 -08:00
H. Peter Anvin (Intel)
acfeb7df6c zlib: pass 15 not 0 to inflateInit2()
The convention of passing 0 to inflateInit2() to autodetect the window
size is not supported in really old versions of zlib. The only
downside with simply passing in the maximum value (15) is potential
additional memory buffer allocations, but it is a drop in the bucket
for NASM.

Fixes: https://github.com/netwide-assembler/nasm/issues/165
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-11-04 00:38:17 -08:00
H. Peter Anvin (Intel)
4709cfe493 Show instruction mismatch errors if another error terminates assembly
Instruction mismatch errors have been held until the last assembly
pass in case changed %if statements cause a code path to be elided in
subsequent passes. However, it is confusing to the user if error
messages aren't shown if another error terminates assembly.

Use the already existing mechanism for warnings to hold the messages
unless another error terminates assembly.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-15 22:32:36 -07:00
H. Peter Anvin (Intel)
1b939ecf3d errors: add the ability to hold errors, not just warnings
Re-introduce ERR_HOLD, which means that an error is treated like a
warning except for the last pass, but unlike ERR_PASS2 the error
message *will* be issued if another error happens on the same pass, just
like warnings.

This will be used to improve error messages on instruction mismatch.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-15 21:09:05 -07:00
H. Peter Anvin (Intel)
efe066ef40 nasm.c: if enabled, print a pass report even on failure
If printing a pass report is enabled, then do so even if the assembly
session failed -- except for a critical or panic failure (e.g. if
running out of memory, which could cause recursive failures.)

This can help discovering e.g. why certain error messages don't appear
when one would expect them (because they have not been detected in the
current pass.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-15 17:34:42 -07:00
H. Peter Anvin (Intel)
f520436a7a Make Watcom workarounds a little less obnoxious
Duplicated code is asking for trouble. Make the Watcom brain damage
workarounds at least patternized.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-15 17:34:10 -07:00
H. Peter Anvin (Intel)
44ec97993a compiler: add and use unreachable() macro
C23 defines unreachable() as a macro in <stddef.h>. For earlier
versions of gcc, __builtin_unreachable() is possible.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-13 17:53:42 -07:00
H. Peter Anvin (Intel)
a7457e66cf Fix matching of branch instructions with prefixes and sizes
Matching of branch instructions with prefixes and sizes is, to say the
least, tricky. Work through it, and add a new macro to help.

Fixes: https://github.com/netwide-assembler/nasm/issues/144
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-10 13:07:46 -07:00
H. Peter Anvin (Intel)
c0aec6969b directives: correctly handle quoted strings in directives
Quoted strings should be valid inside directives, including ones that
contain a ] character. Also check for garbage on the line after the
directive.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-10 10:41:09 -07:00
H. Peter Anvin (Intel)
4cdb62ffb6 listing: modify active_list_options, not just list_options
If the assembly stops after the first pass, only active_list_options
will be seen; this is also the value that applies for local changes in
the list options. Thus, both variables need to be tweaked.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-09 20:09:33 -07:00
H. Peter Anvin (Intel)
7e82d9d1e3 Add a default-error warning for impossible o64, a64, or a16 prefixes
The o64, a64 and a16 prefixes are only possible in specific
modes. It is a lot easier to debug with a more sensible error message.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-09 19:24:07 -07:00
H. Peter Anvin (Intel)
3d96ea651a assemble: weaken the test for "absolute address can not be RIP-relative"
The test for "absolute address can not be RIP-relative" is too strict
and causes false positive failures.

Fixes: https://github.com/netwide-assembler/nasm/issues/145
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-09 16:10:35 -07:00
Nico Weber
a8a1dc2b0c Fix Wbitwise-instead-of-logical warning
`a || b` only evaluates b if a is false. `a | b` always evaluates
both a and b. If a and b are of type bool, || is usually what you
want, so clang now warns on `|` where both arguments are of type bool.

This warning fires once in nasm. It looks like `|` is an (inconsequential)
typo of `||`, so use that instead.

No intended behavior change.
2025-10-09 15:48:18 -07:00
KO Myung-Hun
29a5aabd7a Fix compilation with Open Watcom
Open Watcom does not support 64-bit constants at 'case'.

[ hpa: I'm pulling this, but I'm really, *really* questioning its
supportability long term. The OpenWatcom people need to fix this, or
we are just going to have to say "OW is not supported."

At some point we *are* going to move to a "C99 is baseline" policy for
code; there are simply too many features in C99 that are actively
painful to be without.

That is, unless we decide to go to C++, which is under consideration
but is a much bigger job. In that case, the target will probably be
either C++11 or C++14 as those C++ versions contain some pretty
essential features. ]

Signed-off-by: KO Myung-Hun <komh78@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-09 14:52:29 -07:00
H. Peter Anvin (Intel)
ae9335a0b9 labels: make the prefix/suffix options and pragmas consistent
Make the spellings for the label-mangling options and pragmas
consistent, and implement the directive forms which were documented
but never implemented.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 18:46:39 -07:00
hongjinghao
e39b856bde preproc: fix heap memory overflow CVE-2023-31722
paramlen has heap memory of length nparam+1. The value of variable i
may be greater than nparam+1, causing heap memory overflow. Therefore,
i and nparam+1 needs to be determined in the loop.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392857#c1
Fixes: https://github.com/netwide-assembler/nasm/pull/83
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 14:58:03 -07:00
H. Peter Anvin (Intel)
6e221c8193 assemble: reduce the priority of "mask not permitted" error
Per BR 75, the "mask not permitted" error is a little too persistent.

Fixes: https://github.com/netwide-assembler/nasm/pull/75 (hopefully)
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 11:12:43 -07:00
H. Peter Anvin (Intel)
0a1a9789ec Fix confusion between "postfix" and "suffix"
"suffix" is the more common English term, but NASM used "postfix" for
a long time.  Allow them as aliases, and tidy up the documentation
accordingly.  While making these changes, tidy up the handling of
options in the index.

Fixes: https://github.com/netwide-assembler/nasm/pull/61
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 10:49:55 -07:00
H. Peter Anvin
fa43d8e01d asm: new --bits option to set the processor mode on the command line
This is really just a shorthand for the --before option, but it is
really convenient for quickly writing multimode tests.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 09:56:20 -07:00
H. Peter Anvin
2c63ab038c asm/preproc.c: generalize pp_pre_command()
Generalize pp_pre_command() so that the first argument doesn't have to
be a preprocessor token. Instead, the two arguments are now both
turned into token streams, separated by a whitespace token.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 09:53:15 -07:00
H. Peter Anvin
f1b6d3188c preproc: add a %selbits() function
Although one can implement this "manually" in terms of %sel(), this
function is *really* useful for making multi-mode tests and allows for
better error checking.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 09:17:59 -07:00
H. Peter Anvin
e4044cfc48 assemble.c: avoid a panic after invalid instruction error
If an instruction triggers an error (nasm_nonfatal) it is legitimate
for calcsize() to return 0; in that case no further processing is
needed.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 09:16:06 -07:00
H. Peter Anvin
6df250aee3 parser: add the actual name of a label to the label-orphan warning
The label-orphan warning is *way* more useful if it includes the
actual label name; this way the programmer can usually spot
immediately if it is a label or misspelled instruction.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-07 08:30:02 -07:00
Ivan Sorokin
bd7185bad1 fix undefined behavior in count_mmac_params
When compiled -fsanitize=undefined nasm produced this error message:

asm/preproc.c:2523:25: runtime error: member access within null pointer of type 'struct Token'

The problem is reproducible on tests avx512f, avx512cd, avx512pf
and avx512er in the test suite.

The problematic line was:

    /* Advance to the next comma */
    maybe_comma = &t->next;                            <<< HERE
    while (tok_isnt(t, ',')) {
        if (!tok_white(t))
            comma = NULL; /* Non-empty parameter */
        maybe_comma = &t->next;
        t = t->next;
    }

When t is NULL this line doesn't cause memory access, but it is still an
undefined behavior according to C standard.

I believe that the underlying problem is that this loop doesn't have a sound
invariant about maybe_comma:

* On first iteration: *maybe_comma == t->next
* On the following iterations: *maybe_comma == t

I don't know what the intended loop invariant is and I decided to just
mechanically fix the deferencing of NULL pointer, completely preserving
the existing behavior.

Signed-off-by: Ivan Sorokin <vanyacpp@gmail.com>
2025-10-06 14:56:45 -07:00
Ivan Sorokin
ad297258c1 fix memory leak in eval_strfunc
When running with -fsanitize=leak enabled nasm prints this error:

==19965==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 360 byte(s) in 90 object(s) allocated from:
    #0 0x7faee9396867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5645d39a401c in nasm_malloc nasmlib/alloc.c:55
    #2 0x5645d3a41f9d in string_transform asm/strfunc.c:356
    #3 0x5645d3a37d40 in eval_strfunc asm/eval.c:761
    #4 0x5645d3a37d40 in expr6 asm/eval.c:906
    #5 0x5645d3a3968d in expr5 asm/eval.c:627
    #6 0x5645d3a39aca in expr4 asm/eval.c:602
    #7 0x5645d3a39b72 in expr3 asm/eval.c:563
    #8 0x5645d3a39db8 in expr2 asm/eval.c:537
    #9 0x5645d3a39f38 in expr1 asm/eval.c:511
    #10 0x5645d3a3a0b8 in expr0 asm/eval.c:485
    #11 0x5645d3a3a242 in rexp3 asm/eval.c:422
    #12 0x5645d3a3a508 in rexp2 asm/eval.c:396
    #13 0x5645d3a3a6a8 in rexp1 asm/eval.c:369
    #14 0x5645d3a3a838 in rexp0 asm/eval.c:342
    #15 0x5645d3a3a838 in cexpr asm/eval.c:305
    #16 0x5645d3a3ad08 in bexpr asm/eval.c:298
    #17 0x5645d3a3ad08 in evaluate asm/eval.c:1032
    #18 0x5645d39e4f20 in parse_line asm/parser.c:959
    #19 0x5645d399e243 in assemble_file asm/nasm.c:1735
    #20 0x5645d3998801 in main asm/nasm.c:719
    #21 0x7faee8aaed8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #22 0x7faee8aaee3f in __libc_start_main_impl ../csu/libc-start.c:392
    #23 0x5645d399acd4 in _start (/home/ivan/d/nasm/nasm+0x2e5cd4)

SUMMARY: AddressSanitizer: 360 byte(s) leaked in 90 allocation(s).

This problem is reproducible on test utf.asm.

The problem was caused by the fact that eval_strfunc doesn't free the string
allocated by string_transform.

Signed-off-by: Ivan Sorokin <vanyacpp@gmail.com>
2025-10-06 14:50:40 -07:00
Elyes Haouas
f744ec431f Remove trailing semicolon in macros
Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
[ hpa: only one of the defined issues was valid, removed the rest. ]
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2025-10-06 14:26:42 -07:00
H. Peter Anvin
5b9099c058 asm/parser: print the actual token in an error, not its value
Printing an internal token value is not very user-friendly :) Print
the contents of the actual token in question.

Fixes: https://github.com/netwide-assembler/nasm/pull/90
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 14:07:33 -07:00
H. Peter Anvin
11c512850e warnings.pl: tidy up the formatting of the warning output
Clean up the formatting of the generated warning files.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 12:07:23 -07:00
H. Peter Anvin
2255ebb549 preproc: don't evaluate an %if condition if suppressed
When a line is suppressed there is no guarantee that it is
syntactically valid, so treat it exactly like a dead %if branch.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 09:20:30 -07:00
H. Peter Anvin
cd5dfb8c5f warnings: always regenerate outputs; tidy up unnecessary rules
With warnings disaggregated, there is no reason to play games with not
updating the output timestamp. Always write the files as usual.

Remove unnecessary Makefile rules.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
1# Please enter the commit message for your changes. Lines starting
2025-10-04 14:04:31 -07:00
H. Peter Anvin
a8bcdb641b Warnings: disaggregate from source and tidy up documentation
The idea of putting the warnings in the source code was a nice one,
really, but it ended up being a nightmare from the perspective of
build dependencies. Disaggregate them, and tweak the documentation for
easier reading.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-04 13:46:39 -07:00
H. Peter Anvin
bb3a51e5d6 warnings: remove stray whitespace before warning specification
The actual warning specification should begin immediately after *!.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-04 12:09:05 -07:00
Fabian Giesen
0ee113ce68 Don't assume pragma directives are a single word
pragma->tail is described as "anything after the operation", but
existing parsing passed just the first whitespace-delimited
word. Change the parsing to just strip leading and trailing
white space off the rest of the line, but keep interior spaces
if there are any.

This is preparation for a build_version pragma for Mach-O matching
the llvm-as .build_version syntax.

Signed-off-by: Fabian Giesen <fabian.giesen@epicgames.com>
2025-10-03 10:47:25 -07:00
Zachary Turner
6f48f8f7b7 Support nested response files.
This allows response files specified with -@ to reference other
response files.  This keeps parity with many other common toolchains
such as MSVC, GCC, and Clang which all support nested response
files.

Signed-off-by: Zachary Turner <zturner@roblox.com>
2025-10-03 10:31:12 -07:00
thesmartwon
28b96452a1 fix typo in --help 2025-10-03 10:14:26 -07:00
H. Peter Anvin
0b3c971f5b portability: "const macros_t" is redundant
Some C compilers don't like that...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-01 13:55:38 -07:00
H. Peter Anvin
23ce05f906 treewide: replace verbose copyright headers with SPDX tags
SPDX is an international standard for documenting software license
requirements. Remove the existing headers and replace with a brief
SPDX preamble.

See: https://spdx.dev/use/specifications/

The script used to convert the files is added to "tools", and the
file header templates in headers/ are updated.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-01 11:45:31 -07:00
H. Peter Anvin
595afc6f4b portability: add missing PRI* constants, signed 1 in bitfield hack
Add missing uses of PRI constants.

Create a PRI constant for size_t, since %z isn't available on all
platforms. Notably, the legacy Windows runtime needs %I instead of %z.
Use that on UCRT as well, since there doesn't seem to be a way to
determine if you are compiling for MSVCRT or UCRT.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-30 09:41:03 -07:00
H. Peter Anvin
1185c1f118 Document the [LIST] directive and add -LF option to suppress it
- Document the [LIST] directive
- Add an option -LF to ignore the [LIST] directive
- Fix [LIST -] suppressing output on a following pass
- Minor other documentation fixes

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-29 21:48:43 -07:00
H. Peter Anvin
12f6270124 asm: better error messages for missing instructions
The assembler can't know if something is a colonless label or a
misspelled instruction, so print both when complaining about a missing
instruction.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-29 16:09:04 -07:00
H. Peter Anvin
fb9afe4226 build: more fixes for building in a separate build directory
- Fix additional broken rules
- Add some more files to WARNOBJ
- $^ is not supported by POSIX, apparently
- Update warnings.pl to *actually* use the srcdir and list of
  passed-in files.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-25 13:20:29 -07:00
H. Peter Anvin
e8a0c32ba7 DFV: allow {dfv=pf} as an alias for {dfv=cf}; EQU {dfv}
PF and CF are always set to the same value; allow the programmer to
specify either or both.

Allow EQU to take a {dfv} expression without needing parens.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-23 11:56:31 -07:00
H. Peter Anvin
c4ed07830a Tidy up the morass of EVEX bitfield constants
The EVEX bitfields are complicated at best. Tidy them up to the
best of our ability.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-20 18:26:27 -07:00
H. Peter Anvin
5d75c20105 asm: drop support for ER|SAE in 256-bit vectors
An earlier version of the AVX10.2 documentation had support for ER and
SAE in 256-bit vectors using a special encoding. This was never
implemented, and has since been removed from the spec.

Remove from NASM, too.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-20 17:35:13 -07:00
H. Peter Anvin
c714d66d34 Fixes for JMP|CALL near, RETF, and segment instructions
Some weird things happened when macroizing.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 18:55:54 -07:00
H. Peter Anvin
49fba5a300 labels: can only promote a *defined* EXTERN to GLOBAL
LBL_LOCAL doesn't necessarily mean defined; have to check the defined
flag explicitly.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 17:01:59 -07:00
H. Peter Anvin
a0396faf3b Fix control/debug register patterns
The control and debug registers are always using the default operand
size. It is probably easiest to just encode it explicitly for now.

Control registers are particularly weird because of the AMD "lock as
REX.R" hack...

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 16:26:35 -07:00
H. Peter Anvin
d2aed9fa9d labels: handle "extern" after definition (BR 3392924)
Using "extern" or "required" after the definition should be
interpreted as "global", just as if "extern" or "required" had been
specified before the definition.

Unfortunately the code did not correctly handle the case of upgrading
from LOCAL to GLOBAL via an EXTERN or REQUIRED directive, only from
EXTERN or REQUIRED to GLOBAL via definition or a GLOBAL or COMMON
directive.

Fix.

Reported-by: E. C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 14:22:29 -07:00
Maciej Wieczor-Retman
f0efb28d98 assemble: apx: Add NF forbidden flag and fix SBB and ADC
ADC and SBB don't support using the {nf} prefix. They are the only one
in the arithmetic instructions group that are this way.

Add a flag that will warn when an instructions wants to use {nf} but
doesnt' support it.

Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
2025-09-19 14:53:04 +02:00
H. Peter Anvin
55dd65cddc portability: speed and modernize autoconf; compiler.h improvements
- Add some features to autoconf that makes it cleaner and faster
- Modernize some of the autoconf macros
- Update compiler.h with some C23 features

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-16 13:25:39 -07:00