Reputation: 2734
I want to embed a binary file in an executable, in a way that is portable to both Windows and Linux.
For achieving this, I want to use an assembly file with the directive incbin
. For example:
.intel_syntax noprefix
.global _img0_start
.global _img0_end
.global _img0_size
_img0_start: .incbin "img0.bin"
_img0_end:
_img0_size: .quad $ - _img0_start
I can't know where the binary files will be before compiling the above assembly file, so I want to replace img0.bin
by a macro, and define it within the command line arguments as if I were using the gcc
option -D
for defining a macro constant.
I have seen that there exists the -d Option: Pre-Define a Macro
, that allows to do exactly the above but only for numeric values (I need to provide a path to a file). (font: https://www.nasm.us/doc/nasmdoc2.html#section-2.1.19)
Is there anything like -d
, but that allows to define strings, and works for moth gas
and nasm
?
Upvotes: 2
Views: 1352
Reputation: 364210
NASM macros and the C preprocessor both allow arbitrary strings.
NASM equ
constants have to be integers, but %define foo eax
or nasm '-DIMG="foo.bin"'
both work.
Notice that the macro definition includes the double quotes. CPP makes it easy to expand a macro inside double quotes, but I didn't bother to check if NASM can easily do that.
It's normally easy to create a double-quoted string like that in Make or whatever.
;;; foo-nasm.asm NASM source
incbin IMG
# foo-gas.S GAS source (capital S gets GCC to run it through CPP)
.incbin IMG
hi.bin
contains B8 68 69 00 00
, the 32/64-bit machine code for mov eax, 'hi'
$ gcc -c foo-gas.S -DIMG='"hi.bin"'
$ nasm -felf64 foo-nasm.asm -DIMG='"hi.bin"'
$ disas foo-gas.o
...
0000000000000000 <.text>:
0: b8 68 69 00 00 mov eax,0x6968
$ disas foo-nasm.o
...
0000000000000000 <.text>:
0: b8 68 69 00 00 mov eax,0x6968
Upvotes: 1
Reputation: 2734
I have read more and I have realized that there is no need for a macro that expands to a string.
Instead, what can be done is just providing a fixed name for the binary file, in my case, img0.bin
, and then use the -I
option for providing extra paths for searching such file.
In other words, with the same assembly file as above (let it be assembly_file.s
), just do:
gcc -c assembly_file.s -I<path_to_folder_that_contains_img0.bin> -o <output_file_name>
Upvotes: 2