clang as an x64 assembler

I’m interested in using clang as an x64 assembler, partly because it together with lldb seems to be the only way to debug assembly on a Mac. I wondered if someone could answer a few questions.

I can’t find any documentation about clang and assembly. Through experimentation, I have discovered that this works:

$ clang -g -c -x assembler hello.s
$ clang hello.o -o hello
$ ./hello
Hello, world!

Are these commands (and other relevant command line options) documented somewhere? Is the supported assembly syntax documented somewhere (it seems to support GAS syntax; is it reasonable to use the GAS manual as a reference?)?

Is this recommended? That is, is the integrated assembler intended to be used to write assembly code for a production system in this way, and is it reasonable to rely on it continuing to work in the future?

Thanks.

Mike

Hi Mike,

Are these commands (and other relevant command line options) documented
somewhere?

The "-x assembler" shouldn't be needed. It's assumed for files ending
in .s. I don't think it's really documented anywhere, it's just that
.s files are one of the inputs Clang accepts.

Is the supported assembly syntax documented somewhere (it seems
to support GAS syntax; is it reasonable to use the GAS manual as a
reference?)?

It's broadly GNU syntax, though the concepts available in MachO differ
from ELF so some care is needed using the GNU manuals. For example on
AArch64, Linux you'd materialize a global address like this:

    adrp x0, some_var
    add x0, x0, :lo12:some_var

but targeting iOS you'd write:

    adrp x0, some_var@PAGE
    add x0, x0, some_var@PAGEOFF

Still, many/most GNU directives are cross-platform and we support
those in the way you'd expect (or we should do). And I'd also expect
GNU as to follow the same syntax if it was targeting Darwin.

I usually check Clang's output from a C file whenever I can't remember
what the right directives are.

Is this recommended? That is, is the integrated assembler intended to be
used to write assembly code for a production system in this way, and is it
reasonable to rely on it continuing to work in the future?

Finally I can give some good news! This is fully supported and pretty
stable. There's loads of assembly files out there in the wild which
no-one is going to break.

Cheers.

Tim.