Commit graph

726 commits

Author SHA1 Message Date
H. Peter Anvin (Intel)
b2734a91bd output/outobj.c: free the section name on cleanup
It is a completely unimportant memory leak as the process is about to
exit, but it is easy to clean up.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-07-07 15:43:40 -07:00
Joshua Watt
851460a10b Add --debug-prefix-map option
Adds an option to remap file prefixes in output object files. This is
analogous to the "-fdebug-prefix-map" option in GCC, and allows files to
be built in a reproducible manner regardless of the build directory.

[ hpa: this still needs to be documented in doc/running.src. ]

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-07-04 19:22:48 -07:00
Nick Clifton
8890d723d0 outobj: fix buffer overflow when too many segments in a group
The number of entries in a group was hard-coded, but not enforced.

Reported-by: <BreakingBad6@github.com>
Fixes: https://github.com/netwide-assembler/nasm/issues/203
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 18:22:44 -07:00
H. Peter Anvin (Intel)
dad1d8ca42 outbin: register the mapfile; check for input clobber; remove on fail
- Register the map file with the filename system.
- Check to see if the mapfile is clobbering the input.
- Remove the map file if the output file is also removed.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 18:22:39 -07:00
H. Peter Anvin (Intel)
720a32672c Refactor the handling of primary input/output files
Replace the direct setting of variables for input and output variables
with accessors. This allows for properly tracking the lifetimes of the
data and allows for things like checking of the overwrite of the
primary input file to be centralized.

It isn't possible *in the general case* to check for overwrite of
*any* of the input files, although in the particularly important case
of the assembler proper it ought to be possible to do a bit better:

it should be able to guard for overwrites of non-primary input files
except for the error file or the list file if and only if -Lp is used.

That is, however, a latter project.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-29 18:22:26 -07:00
H. Peter Anvin (Intel)
5b9dce55cb outas86.c: fix memory leak during initialization
It is just an O(1) memory leak but might as well fix it.
filename_set_extension() returns a newly allocated string,
as86_add_string() does not consume a string reference, so the string
is left danging at the end; add nasm_free() to free it.

Reported-by: <for-just-we@github.com>
Fixes: https://github.com/netwide-assembler/nasm/issues/255
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-28 16:40:17 -07:00
SEt
dee2d13f2c Proper handling of several code sections in Codeview debug information
Current Codeview implementation assumes there is only one actual code
section and all line number information is pulled there. With support
of comdat that assumption becomes incorrect, so proper generation of
line number information table per code section is required.

Also solved an issue with incorrect relocation for symbol with the
same name as some section.

New code assumes that coff_nsects doesn't change during generation of
debug information.

Fixes: https://github.com/netwide-assembler/nasm/pull/183
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:17:36 -07:00
Kacper Michajłow
1c9e16a03f output/codeview: don't panic when there is not code section
Simply skip the tables that require code section and source information.

Fixes: https://github.com/netwide-assembler/nasm/issues/216
Fixes: https://github.com/netwide-assembler/nasm/pull/178#issuecomment-4156226648
Signed-off-by: Kacper Michajłow <kasper93@gmail.com>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:13:27 -07:00
knut st. osmundsen
c8be7b7a3f outcoff.c: Added support for 'function' type on global symbols.
Setting the symbol type is necessary for the control flow guard (CFG)
stuff on windows.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:08:03 -07:00
knut st. osmundsen
3ad8e6c533 outcoff.c: Prevent elimination of extern safeseh symbol.
Do a lookup of the symbol given to safeseh to prevent out_symdef() from
thinking it is unused.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:06:49 -07:00
knut st. osmundsen
06d2606b06 outcoff.c: fix warnings
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:05:20 -07:00
knut st. osmundsen
f1e4e6fe16 outcoff.c: dd symbol wrt ..symtab
Added a special symbol ..symtab for emitting the COFF symbol table
index of a symbol rather than some kind of address.  For use with ehcont
metadata and possible other stuff.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-06-07 20:05:20 -07:00
Yongjie2017
498afdba1b output/outelf.c: fix address sanitizer findings in elf output format
In elf_init, cur_path is a malloced one, and should be freed.

This patch adds a free operation by the end of the elf_init.

Signed-off-by: Yongjie Sheng (Intel) <sheng.yongjie@outlook.com>
2026-04-17 06:38:10 +08:00
bonsthie
207b3727c7 elf: add support for GNU indirect function symbol type (gnu_ifunc)
This feature enables emission of IFUNC symbols for global directive.

Example syntax:
```asm
    global  func_ifunc:function

    global  func:gnu_ifunc
    func    equ func_ifunc
```
2026-04-09 18:23:57 -07:00
H. Peter Anvin (Intel)
93c8f343d9 output/outform.c: change "Legacy alias" -> "Alias" in help text
Aliases aren't necessarily "legacy".

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-04-06 18:16:10 -07:00
H. Peter Anvin (Intel)
6b0e02de3a outform.h: add "omf", "omf2" and "os2" as aliases for obj[2]
Add aliases for the OMF object code formats.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2026-04-06 18:14:56 -07:00
SEt
b8c2a63286 Fix several issues with Codeview debug information 2025-12-09 16:44:35 -08:00
Petr Sumbera
ca178f80d8 Fix for big endian platforms.
https://bugzilla.nasm.us/show_bug.cgi?id=3392745
2025-11-04 10:14:49 -08:00
Alexey Vishnyakov
891c63e64f output: fix null pointer dereferences in output/outaout.c
The bugs were found by Svace static analysis tool:

1. sym can be null in when exact is false, and sym is later dereferenced
   by sym->symnum
2. asym can be null, no return from function is performed, and asym is
   dereferenced by asym->symnum
2025-10-06 14:58:40 -07:00
Elyes Haouas
fc720e7040 Remove useless parentheses in return statements
Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 14:28:54 -07:00
Elyes Haouas
d9494d5240 output/outmacho.c: Add missing space at break quoted strings
Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
2025-10-06 14:22:59 -07:00
H. Peter Anvin
81dd083291 outelf.c: remove stray semicolon
Harmless, but undesirable.

Fixes: https://github.com/netwide-assembler/nasm/pull/57
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-10-06 14:19:15 -07:00
KO Myung-Hun
c7e17f5dde outobj: make a group cumulative
On any other OMF assemblers such as MASM, TASM and ALP, a group is
cumulative.

Signed-off-by: KO Myung-Hun <komh78@gmail.com>
2025-10-06 13:24:09 -07:00
KO Myung-Hun
c7f426f5e5 outobj: add obj2 driver
Obj2 is an obj variation for OS/2.

Obj2 has the following differences from obj.

1. Default attributes for a segment are ALIGN=16 and USE32.
2. Add a segment to FLAT group implicitly if 32-bit segment.
3. Recognize Unix sections .text, .rodata, .data and .bss as TEXT32,
   CONST32, DATA32 and BSS32 respectively for compatibility with other
   Unix platforms.
4. Set default classes implicitly for known segments such as TEXT32,
   CONST32, DATA32, BSS32 and so on.

Signed-off-by: KO Myung-Hun <komh78@gmail.com>
2025-10-04 15:57:24 -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
Fabian Giesen
83655f041e build_version pragma + macro for Mach-O
Matches the llvm-as .build_version syntax. Newer MacOS linker
complains when object files don't contain a LC_BUILD_VERSION.

Signed-off-by: Fabian Giesen <fabian.giesen@epicgames.com>
2025-10-03 10:47:27 -07:00
Fabian Giesen
9e03a95922 elf: change form for DW_AT_frame_base to DW_form_exprloc
data4 was not spec-compliant here, causing some tools reading
DWARF debug info to complain. Thanks to Nikita Smith for
debugging this!

Signed-off-by: Fabian Giesen <fabian.giesen@epicgames.com>
2025-10-03 10:30:55 -07:00
KO Myung-Hun
05343069fa outobj: disallow the combination of USE16(16-bit segment) and FLAT
FLAT should be used only with USE32(32-bit segment).

Signed-off-by: KO Myung-Hun <komh78@gmail.com>
2025-10-03 10:27:39 -07:00
KO Myung-Hun
3f37cacdd2 outobj: allow for segments in the group FLAT to belong to the other group
It's possible that segments belong to both the group FLAT and the other
group because the group FLAT is a pseudo group.

Signed-off-by: KO Myung-Hun <komh78@gmail.com>
2025-10-03 10:27:39 -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
e2bff29935 outbin: make map file unwritable an error
If the user requests a map file, the user needs to be able to
get it!

It should have been an error, not a warning, all along.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-19 14:27:22 -07:00
H. Peter Anvin
dc247c9f99 Speed up autoconf, use POSIX names instead of cpu_to_le*()
The POSIX names for these functions are htole*(). Use those
preferentially.

Speed up autoconf by allowing early-out during alternative function
searches.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-15 18:26:25 -07:00
H. Peter Anvin
d79fb158e1 outobj: make sure segment alignment warnings are kept
Warnings are flushed at the end of each pass, but the segment
directive in outobj is only processed once. Therefore, keep track of
the originally requested alignment size so the warning can be
re-issued on later passes.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-05 22:10:18 -07:00
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
6a270222be outobj: fix crash when no labels are defined in the default segment
If there are no labels in the default segment, then NASM would crash
if anything was emitted in it, as the label would be defined on the
final pass only.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-04 20:06:06 -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
ece92ba533 output/legacy.c: remove stale file
output/legacy.c is no longer used, remove it.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-09-03 10:33:59 -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
83534f1f37 Merge remote-tracking branch 'origin/master' into apx.wip
Resolved Conflicts:
	Makefile.in
	Mkfiles/msvc.mak
	Mkfiles/openwcom.mak
	asm/assemble.c
	asm/nasm.c
	asm/parser.c
	doc/Makefile.in
	include/nasm.h
	include/tables.h
	output/legacy.c
	travis/test/br3392531.stderr
	travis/test/br3392716.stderr
	travis/test/org.stderr
	x86/insns.dat

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-25 21:09:15 -07:00
H. Peter Anvin
2726aefb06 output: remove the legacy output entry point
Remove the legacy output entry point. It has proven impossible to find
the time to completely port the backends all at once.

Instead, always generate the legacy output data, but put them into the
out_data structure. Then add a macro to explode these arguments into
separate variables, equivalent to the old function arguments. This
also centralizes the type definitions for these variables.

Most importantly, it means that the entire struct out_data is now
always available, which means that backends that need the additional
information available in that structure, such as the specific
instruction template, can access that information without needing to
revamp the entire backend code all at once.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2025-08-13 13:51:25 -07:00
H. Peter Anvin
e515dac43f More matching/macrofication work; now passes "make travis"
More matching and macrofication work.
Improve some error and warning messages.
Update some travis tests for better messages and added optimizations.

Fix duplicated warning messages for the same out-of-range value
problem.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2024-08-22 22:07:31 -07:00
H. Peter Anvin
75f6f4cdb2 WIP: more matching and template work
Further work on a better matching system. Still a work in progress,
however.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
2024-08-20 12:59:07 -07:00
H. Peter Anvin
bff94fbd39 Major changes to a number of subsystems to improve matching
Work through a number of changes toward making matching a lot saner,
both to reduce the number of patterns to generate for APX but also to
make a number of code patterns simpler.

This replaces a fair number of byte codes.

Improve a number of error messages, especially related to overflows.

Move process_insn() from nasm.c to assemble.c, as it really is the
primary entry point to the assembler module.

Reorder some prefixes. In particular, F2/F3 override 66 when used as a
mandatory prefix, so it makes more sense for them to be closer to the
opcode.

Move a lot more information into struct insn. It is better to have it
in one place; memory consumption is not an issue because struct insn
is transient information.

Get rid of "optimization levels" and replace it with a mask of
flags. That was already halfway done; complete the job.

Replace seg:offset in struct out_data with a struct location. It would
be better to extend this to more places, too.

The ARx and SMx flags are now explicit bitmasks, instead of having a
couple of hard-coded ranges.

Add __func__ to assert or panic messages.

Because of prefix and message changes, a number of travis tests had to
be audited and updated.

Fix a number of instruction patterns which had .128 when they ought to
be .lig. This is no longer a minor issue with the disassembler: for
AVX10, the pattern vector length determines how SAE/RC are encoded,
and there is no valid 128-bit encoding. However, with .lig the 512-bit
encoding can be used.

Separate "o64nw" into two pieces: opsize 64 and "nw" = "REX.w not necessary". The
latter can be included in non-64-bit patterns. "o64" still set REX.W
since that is still the common thing.

New "osz" bytecode: emit an OSP *or* REX.W depending on the current
mode and operand size. Useful for special cases like "nop" where "o64
nop" probably wants to be encoded as "48 90".

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-08-07 17:13:44 -07:00
H. Peter Anvin
7cc9fc4773 stdscan: abstract out the saving/restoring of parser state
The parser state does not just necessarily include the position of the
buffer, but make it possible to maintain additional state.

Furthermore, add an explicit ability to push back a token.

All of this might make it easier at some point in the future to keep
track of horizontal position, although that will require lots of
changes to the preprocessor.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2024-07-24 14:15:35 -07:00
H. Peter Anvin
21c977e717 output/legacy: when converting OUT_SEGMENT, clear the offset
Legacy backends like obj/omf may depend on the offset being zero if
not in use.

Reported-by: Bernd Boeckmann <bernd-freedos@boeckmann.io>
Reported-by: Vladimir Chebotarev <vladimir.chebotarev@gmail.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2023-10-16 17:19:45 -07:00
H. Peter Anvin
cab5b75145 output: when converting OUT_SEGMENT to legacy, don't lose the addend
When converting an OUT_SEGMENT relocation to legacy, the addend is
still significant, as it would represent an explicit addend to the
segment number.

Fixes: https://bugzilla.nasm.us/show_bug.cgi?id=3392783
Reported-by: David Fifield <david@bamsoftware.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-12-07 10:51:33 -08:00
H. Peter Anvin
c8af731120 outieee: fix segfault on empty input
Fix the IEEE backend crashing if the input file is empty.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-12-07 10:24:14 -08:00
H. Peter Anvin
93548c2de2 rdoff: kill it off
RDOFF has been deprecated as it has unfixable problems, and has been
announced that it will be killed off in NASM 2.16.

This deletes it once and for all.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2022-11-07 16:49:13 -08:00
H. Peter Anvin
ed70640526 Merge remote-tracking branch 'johannkoenig/master' 2022-11-07 12:54:22 -08:00