Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polygot include file for nasm/yasm and C

I have a bunch of magic numbers that I would like to include in both a C program and an assembly file to be compiled by nasm or yasm.

In plain C the file would look something a series of defines, like:

#define BLESS   55378008
#define ANSWER        42
...

In nasm or yasm, the same include could be implemented as:

%define BLESS   55378008
%define ANSWER        42
...

The only difference is that leading character before the define: # for C and % for nasm.

Is there any way to write a polygot include that allows me to include it in both C and nasm and only list the constants once?

Yes, I'm aware that I could just use sed or whatever to generate one file from the other.

like image 611
BeeOnRope Avatar asked Oct 25 '17 00:10

BeeOnRope


1 Answers

NASM by itself has no way to include C header files in assembly code. This has been brought up in the NASM forum through the years. You will need an external tool to parse the C header files into something usable with NASM assembly syntax.

One such 3rd party contribution that is suppose to be compatible with NASM is h2incn. I haven't tested it thoroughly enough so can't say it is stable or usable enough for all use cases.

The alternative is to pre-process the files with other tools like m4, cpp, or even translating with sed

like image 182
Michael Petch Avatar answered Oct 06 '22 13:10

Michael Petch