I wan't to write a kernel module which uses quite a lot of inline assembly. Since I am used to Intel Syntax I would like to avoid AT&T Syntax completely. One way of doing this is shown in the following minimal example:
samplemodule.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
unsigned long foo(void) {
unsigned long ret = 0;
asm (
".intel_syntax noprefix\n"
"mov rbx, 1337\n"
"mov %0, rbx\n"
".att_syntax noprefix\n"
:"=r"(ret)
:
:"rbx"
);
return ret;
}
static int init_routine(void) {
printk(KERN_INFO "Sample Module init\n");
printk(KERN_INFO "Test: %lu\n", foo());
return 0;
}
static void exit_routine(void) {
printk(KERN_INFO "Sample Module exit\n");
}
module_init(init_routine);
module_exit(exit_routine);
Makefile
obj-m += samplemodule.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
So, whenever I want to inline assembly I have two write .intel_syntax noprefix\n...\n.att_syntax noprefix\n
. Are there other ways of accomplishing this? When compiling with gcc I used to simply pass the -masm=intel
argument to gcc which allowed me to freely use Intel Syntax. Is something similar possible in this case?
According to https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt, there are several variables to pass specific options to the gcc toolchain: the one you should try is KBUILD_CFLAGS_MODULE
. In this way, you can still use the option -masm=intel
. Your all
target should be like
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) KBUILD_CFLAGS_MODULE='-masm=intel' modules
Problems arise if you include assembler code from kernel headers (which, I think, is not infrequent), which use AT&T syntax: in that case, the final code will mix both syntaxes, and the compilation will fail. In this last case, I think the only way is to manually specify the Intel syntax in each asm
directive, as you did so far.
Since changing compilation parameters or the scripts is not a clean job and is prone to errors, I would suggest a simple workaround: your main issue is avoiding to type ".intel_syntax noprefix\n"
and ".att_syntax noprefix\n"
every time, right? You can place these directives inside a macro, maybe place the macro in a header and include that header everywhere you need. For example, you can define a macro like
#define INTEL_ASM(a,b,c,d) \
asm ( \
".intel_syntax noprefix\n" \
a \
".att_syntax noprefix\n" \
:b \
:c \
:d \
)
so that your code becomes
unsigned long foo(void) {
unsigned long ret = 0;
INTEL_ASM (
"mov rbx, 1337\n"
"mov %0, rbx\n"
,"=r"(ret)
,
,"rbx"
);
return ret;
}