How do I prevent cloning of code?

I am writing some low-level sandboxing code on Linux, and an unusual
problem came up. I need to write an assembly function that has a
"magic" instruction; and the kernel needs to know about the location
of this instruction.

This is relatively simple to do from an "asm()" instruction; I can
just make it return the address of this instruction. But all of that
fails, if the C/C++ compiler decides to duplicate my assembly code.
The kernel wouldn't know what to do, if there are suddenly multiple of
these "magic" instructions all over the source code.

In gcc, I believe I can put my "asm()" code into a function, and then
mark the function as "noinline" and "noclone". For good measure, I
also mark the assembly as "volatile", but I am not sure that is
strictly necessary.

clang doesn't appear to have a "noclone" attribute. This causes two
problems: a) unconditionally setting the "noclone" attribute results
in a warning, which fails because of -Werror in our build scripts, and
b) I am worried that clang might actually decide to clone my function.

I currently put the function into a separate compilation unit, so
things might turn out OK anyway. But I'd much rather tell the compiler
what I want it to do.

As a last resort, I could move the code into a *.S file (or more
likely, one *.S file per architecture). But since it deals with
variadic arguments, that's a little awkward and probably needs some C
glue anyway. Having everything in a single *.c or *.cc file would be
my preference.


Hi Markus,

The best way to do this is with a file scope inline asm blob. This is half way between a function asm and a .s file.