0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-09-22 10:43:39 -04:00
Commit Graph

251 Commits

Author SHA1 Message Date
H. Peter Anvin
ac93d75da3 Sanitize the handling of messsages; improve info and debug
Make the handling of messages saner. In particular, regularize the
handling of info and debug messages, so that nasm_info() and
nasm_debug() actually become useful.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 12:53:20 -07:00
H. Peter Anvin
31a160759d Add %ifdirective preprocessor directive
Along with C and other languages, the current trend is to be able to
probe for features rather than relying on version numbers. This is
motivated in part by the intent of bumping the major version number to
3.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 19:41:12 -07:00
H. Peter Anvin
ec48f1a3f8 preproc: unbreak %cond()
The previous patch accidentally broke the %cond() function.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 18:23:55 -07:00
H. Peter Anvin
d5bd136ced preproc: add message-generating and %null() functions
Add the %null(), %note(), %warning(), %error(), and %fatal()
functions. They behave like the corresponding directives, then expands
to nothing.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 17:10:42 -07:00
H. Peter Anvin
e80bc631a2 preproc: identify $$ as TOKEN_BASE when tokenizing
$$ is TOKEN_BASE, not a symbol. If this is done incorrectly, ppscan
chokes on $$ as it ends up being an invalid symbol.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 11:33:08 -07:00
H. Peter Anvin
7e8f1d571e preproc: fix generation of macro invocation debug data
It is possible for m->mstk.mmac to point back to itself. In that case,
don't terminate the macro debug invocation just yet.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 10:34:28 -07:00
H. Peter Anvin
a84925df12 $ numeric syntax: require a digit to follow
The documentation actually states that $ as a hex prefix is only valid
when followed by a digit, which at least makes the syntax conflict
less complicated. Actually match the documentation.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 01:05:41 -07:00
H. Peter Anvin
54efdde7b1 preproc: warn for leading $ when a number is consumed in preprocessor
To avoid repeated warnings, there is no warning issued for when
tokenizing a number starting with $ in the preprocessor, but issue a
warning if such a number is *consumed* (used in arithmetic) in the
preprocessor.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 00:46:23 -07:00
H. Peter Anvin
178a1b7443 Add directive to disable dollar-hex constants
The use of $ prefixes for hexadecimal numbers conflicts with
the use of $ to escape symbols.  Add a directive to disable
$ for hexadecimal numbers so that those escapes work OK.

As a result, allow escaped symbols to start with a digit.

Add a warning that this syntax is deprecated.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 00:35:41 -07:00
H. Peter Anvin
e05867ce3d ndisasm: make the assembler (hopefully) work again
- Significantly overhauled the disassembler internals to make
  better use of the information already in the instruction template
  and to reduce the implementation differences with the assembler
- Add APX support to the disassembler
- Fix problem with disassembler truncating addresses of jumps
- Fix generation of invalid EAs in 16-bit mode
- Fix array overrun for types in a few modules
- Fix invalid ND flag on near JMP

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-30 16:16:43 -07:00
H. Peter Anvin
9ea92eab6e preproc: reference counting for mmacros, minor cleanups
The late cleanup of macros can cause severe memory hogging with nested
%reps. Instead, implement proper reference counting for mmacros.

Adds some other minor cleanups as well, notably delete_*() are
designed to update or null the pointer that is passed to it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-25 20:11:02 -07:00
H. Peter Anvin
ada267ec8c preproc: let %ifdef test for existence of macro functions
It is good to have a way to test for the existence of macro functions,
and since they are really just a special case of single-line macros,
allow %ifdef to test for them instead of coming up with something
entirely new.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-16 19:15:00 -07:00
H. Peter Anvin
06f7ce2dba preproc: actually use uleb128 format for encoding macros
The format wasn't actually uleb128 because it was accidentally
bigendian (like UTF-8). That is just begging for confusion in the
future, if and when the uleb128 code gets librarized.

Fix it now.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 17:07:15 -07:00
H. Peter Anvin
df4555d68b preproc: compress standard macros with zlib
The very simple compression scheme used for the builtin macro sets no
longer works adequately, and in fact it generates incorrect output
now.

Drop the whole idea of an ad hoc compression scheme and just use
zlib. For the case where there is no system zlib available, include a
(subset of) the zlib distribution. The configure script can be set to
force this included zlib if desired (e.g. for testing.)

Unfortunately this turned out to be a pretty painful can of worms in
terms of complexity. On the other hand having zlib available might be
useful at some point in the future.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 16:51:39 -07:00
H. Peter Anvin
0c533873f0 preproc: add %iffile|%isfile() and %realpath()
Add a function to test for the existence of a file, and a function
query the real operating system path, if available.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-15 00:32:33 -07:00
H. Peter Anvin
0843052f05 preproc: implement %pathsearch() and %depend() as functions
Implement preprocessor function equivalents of the %pathsearch and
%depend directives.

Simplify the incbin standard macro by using these functions.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-14 21:46:55 -07:00
H. Peter Anvin
1da646aeba Merge remote-tracking branch 'origin/nasm-2.16.xx'
Resolved Conflicts:
	asm/preproc.c

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 18:52:20 -07:00
H. Peter Anvin
9e6a4dd028 preproc: inc_fopen(): use the correct path for hashing the fullpath
This code incorrectly would try to use "path" as the hash key instead
of full->path, causing the key in struct hash_insert to diverge from
the one used in hash_add(). Fix that.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 14:42:47 -07:00
H. Peter Anvin
f49e9f1bea preproc: hack around mmacro lifetime problems by deferring free
Under some circumstances, such as:

- Certain uses of %exitrep in syntactically invalid code;
- %unmacro of a *alias* to a macro currently being expanded;

... it is possible for an mmacro to get freed while it is still in
use. Although inefficient, the easiest way to avoid this is to not
free mmacros until the end of pass cleanup, when named mmacros are
also freed.

To support this, use the existing ->next field in the MMacro structure
to keep a list of anonymous or removed MMacros. Don't free ->name at
this point, though, since that is currently used to distinguish
between %rep's and %macro's.  (This needs to be cleaned up to support
constructs such as %while or %for, but that is for later.)

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-11 14:02:12 -07:00
H. Peter Anvin
8d62e99e14 Add %note directive to add a note in the list file
This differs from a plain old comment in the following ways:

1. It is optionally macro-expanded;
2. It has a dash prefix;
3. It can be used inside .nolist macros.

Suggested-by: <pushbx@ulukai.org>
Resolves: https://bugzilla.nasm.us/show_bug.cgi?id=3392915
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-09-19 13:21:30 +02:00
H. Peter Anvin
33ef63669c doc: improve some formatting, mostly of the warning list
Fix some formatting markups, especially with regards to warnings.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-04-04 12:58:32 -07:00
H. Peter Anvin
9f83c383e4 preproc, %map(): require second colon, update documentation
Require the second colon before the grouped parameter count; otherwise
the syntax is ambiguous since an expression can start with (.

Update/complete the documentation and the examples.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 13:42:16 -07:00
H. Peter Anvin
cb96db9b70 preproc: add the ability for %map() to have fixed arguments
Add the ability to have fixed arguments in %map. This is extremely
useful for parameterizing the invoked macro using arguments to a
surrounding macro.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 02:56:13 -07:00
H. Peter Anvin
78bde7562d preproc: factor expand_one_smacro() even more
Separate out counting and parsing smacro parameters into separate
functions. This not only makes the code *way* easier to read, but
these can be re-used e.g. for %map().

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 02:03:56 -07:00
H. Peter Anvin
8584bce804 preproc: handle empty expansion in %map
%map(foo) should expand to the empty string, but instead crashed NASM.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 01:24:20 -07:00
H. Peter Anvin
34eefd3803 preproc: add %map() function and radix specifiers
Add the %map() function which can apply arguments to a macro from a
list.

Allow the user to specify the desired radix for an evaluated
parameter. It doesn't make any direct difference, but can be nice for
debugging or turning into strings.

As part of this, split expand_one_smacro() into two parts: parameter
parsing and macro expansion. This is a very straightforward splitting
of two mostly unrelated pieces of functionality.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-15 22:43:28 -07:00
H. Peter Anvin
2e9120e56f BR 3392893: preproc: correctly handle empty %rep blocks
When expanding %rep blocks, if any of the %rep blocks are empty, there
may be need to unwind the %rep stack multiple times. The code would
not do so -- there was a break; in the loop, which incidentally turned
it into something that wasn't a loop at all.

Reported-by: E. C. Maslock <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 20:51:52 -07:00
H. Peter Anvin
dcac46d973 preproc: add options for a base prefix to %num(), add %hex()
Make it possible to add a base prefix to %num().

Add the %hex() function, producing hexadecimal values that are
nevertheless valid NASM numeric constants.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 17:50:55 -07:00
H. Peter Anvin
e2dc551796 preproc: fix the parameter number in error messages
The user would generally expect the parameter number to be counted
from 1 for human purposes, and that is also consistent with %1, %2,
... for multi-line macros.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-13 17:21:12 -07:00
C. Masloch
d48839780d BR 3392892: fix memory leak related to BR 3392414
The fix for BR 3392414 introduced a fairly serious memory
leak. C. Masloch was kind enough to track down the proper root cause
and fix it correctly.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-11 13:58:48 -07:00
H. Peter Anvin
488d7c7bee preproc: fix memory leak in %[...] processing
"Why dup_tlist() here? We should own it."

Yes, we own it, but we still need to advance the tail pointer. Create
steal_tlist() for this purpose.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392774
Reported-and-Debugged-by: C. Masloch <pushbx@ulukai.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 12:08:07 -08:00
H. Peter Anvin
ae0d289123 preproc: avoid crashing on %exitmacro in broken nesting
Broken %if, %rep and %macro nesting can result in the %exitmacro
unwind overrunning the condition stack. Fix.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392796
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:51:15 -08:00
C. Masloch
65d1eca5c6 preproc: fix incorrect use of nasm_new() in alloc_Token()
The argument to nasm_new() is the pointer, not the indirection from
the pointer. This code is only relevant when compiled without token
recycling (TOKEN_BLOCKSIZE not set), but it is still wrong...

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:26:26 -08:00
C. Masloch
e0959432fe preproc: fix the expansion of %00 (token type change)
Fix the expansion of the %00 mmacro parameter; broken due to a missing
change of the token type.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392803
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 11:21:47 -08:00
H. Peter Anvin
516fbba03e preproc: add conditional-string smacro parameters; simplify functions
Add the option of having strings only conditionally quoted (&&) -- do
not quote an already quoted string again -- as opposed to always
quoting a string.

This makes a lot of the string functions way simpler to implement, and
removes the need to share ad hoc parsing code with directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-21 10:54:19 -08:00
H. Peter Anvin
36cd305ade preproc: add %abs(), modify %num(), refactor
Add the %abs() function, to produce the absolute value as an
always-positive decimal constant.

Change the order of the arguments for %num().

Refactor the handling of optional arguments, to reduce the amount of
redundant code. This is currently only used for builtin functions, but
might be extended in the future.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-17 15:29:10 -08:00
H. Peter Anvin
1d1ba9c7d7 preproc: add %num() to format a number in an arbitrary base
Add the %num() preprocessor function, which returns a quoted string
with a number formatted in any base between 2 and 64 (using bash
encoding with '@' for 62 and '_' for 63.)

It can specify a fixed number of digits with or without truncation.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-15 16:58:20 -08:00
H. Peter Anvin
db6549c5aa preproc: classify warnings, move into common pp-* namespace
Classify all remaining WARN_OTHER warnings in the preprocessor. Move
all preprocessor warnings except "user" under a common pp-* prefix.

Warn for an out-of-range argument to the %sel() function.

Finally, use "dname" in additional places for consistency and future
ease of use.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 13:12:01 -08:00
H. Peter Anvin
664a79473d preproc: add %count() function
Add %count(), giving a count of the number of arguments.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-14 01:48:38 -08:00
H. Peter Anvin
d9b36e3c9c preproc: implement the %cond() and %sel() functions; fix memory leak
Implement the %cond() and %sel() functions that expand to a specific
one of the arguments. %cond(x,y,z) is basically a shorthand for
%sel(2-!(x),y,z) used when x is a boolean condition.

Fix a memory leak in %strcat and %strlen.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 22:59:16 -08:00
H. Peter Anvin
51ad8e1486 preproc: allow preprocessor function expansion to recurse
Allow preprocessor function expansion to recurse. Nearly all the
machinery for recursive smacros was already in place; this merely
activates it for the specific case of preprocessor functions. Making
it a general facility should be deferred to a later relese, though.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-13 20:34:04 -08:00
H. Peter Anvin
3fe5b3f5a1 preproc: distinguish between directives and functions
Some preprocessor functions have the same name as directives. In those
cases, they should be expanded as functions if and only if they are
followed by a left parenthesis. Although it is not inherently true that
either preprocessor functions require a paren nor that directives
cannot start with one, but it is true and will remain true for all
cases where there is a namespace collision.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:51:22 -08:00
H. Peter Anvin
359e21e773 preproc: implement %strlen as a preprocessor function
Implement a %strlen() preprocessor function, equivalent to the %strlen
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:15:35 -08:00
H. Peter Anvin
4150848a7d preproc: implement %substr() and %tok() preprocessor functions
Implement preprocessor functions equivalent to the %substr and %deftok
directive.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 19:02:41 -08:00
H. Peter Anvin
913901e529 preproc: skip invalid advance in %substr
%substr contained a token skip to "skip expanded ID", which is
incorrect, as that has already been skipped at that point. It worked
anyway, accidentally, as this token would always be a whitespace token
-- but we then do skip_white() immediately thereafter.

Delete this to allow this code to be factored.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:28:11 -08:00
H. Peter Anvin
57fbd34d9f preproc: implement %str() and %strcat() functions
Add function equivalents of the %defstr and %strcat directives.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-11 18:18:04 -08:00
Marco Vanotti
6224dd0b45 preproc: don't unmacro if macro cannot be found.
This commit adds a check to see if the macro that we want to unmacro exists.
A previous commit, introduced a check to see if the unmacro was undefining a macro being expanded, but that same check included a null pointer dereference if the macro to undefine did not exist.

The following code reproduced the issue:

```asm
%macro baz 0
  %unmacro F 0
%endmacro
baz
```

Compile with:
```shell
$ nasm -f elf64 -g -FDWARF -o tmp.o -werror file.asm
```

[hpa: adjusted code to match NASM style]

Fixes bug 3392761

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:30:49 -08:00
H. Peter Anvin
1a5fcb070a Revert "preproc: fix memory leak (and possibly CVEs?)"
This reverts commit 8fcc785f95.

This patch causes test a32offs.asm, and in general *any* use of the
"bits" macro, to totally fail.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 13:08:30 -08:00
H. Peter Anvin
b18e870d90 Merge remote-tracking branch 'ElyesH/typos' 2022-11-07 12:39:44 -08:00
H. Peter Anvin
3516258783 Merge remote-tracking branch 'ElyesH/comments' 2022-11-07 12:39:34 -08:00