Search code examples
gccassemblynasmgnu-assembler

Provide incbin path as a predefined macro in both NASM and GCC


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?


Solution

  • 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>