How to assemble x86 assembly code with clang?

Hi i’m trying to assemble some X86 AT&T assembly code into machine code but when i try to execute clang to assemble, it tries to invoked “as” but i don’t have any GNU tools installed. Is there any way to compile it without using “as”? I have tried adding -fuse-ld=lld but that doesn’t seem to work.

The following is the output when running clang for an x86 machine.

PS C:\Documents\Test> clang -c boot.s -v --target=86-pc-none-elf
clang version 16.0.0
Target: 86-pc-none-elf
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 "as" -o boot.o boot.s
clang: error: unable to execute command: program not executable
clang: error: assembler command failed with exit code 1 (use -v to see invocation)

And the following when -fuse-ld=lld is added.

PS C:\Documents\Test> clang -c boot.s -v --target=86-pc-none-elf -fuse-ld=lld
clang version 16.0.0
Target: 86-pc-none-elf
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
clang: warning: argument unused during compilation: '-fuse-ld=lld' [-Wunused-command-line-argument]
 "as" -o boot.o boot.s
clang: error: unable to execute command: program not executable
clang: error: assembler command failed with exit code 1 (use -v to see invocation)

I’d really like to use LLVM without having to install the GNU tools, is it possible?

There is a typo in you target name 86-pc-none-elf. If you fix the target name I think it will automatically enable the integrated assembler and just work. But if not try passing -fintegrated-as to clang.

1 Like

Thank you very much, adding the -fintegrated-as to clang stopped it trying to call “as” :slight_smile:

@tstellar Hey, i’m having the same problem but now when linking the object files. When i run “clang boot.o” it tries to invoke gcc which is really weird.

PS C:\Documents> clang boot.o --target=i386-unknown-unknown-elf -v -fuse-ld=lld
clang version 16.0.0
Target: i386-unknown-unknown-elf
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 "gcc" -fuse-ld=lld -m32 -o a.out boot.o
clang: error: unable to execute command: program not executable
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

I think this is happening, because clang does not recognize the triple. Try --target=i386-unknown-linux-elf.

Thank you for replying, after i changed it to linux, here’s what was output.

PS C:\\Documents\TEST> clang boot.o --target=i386-unknown-linux-elf -march=i386 -v -fuse-ld=lld
clang version 16.0.0
Target: i386-unknown-linux-elf
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 "C:\\Program Files\\LLVM\\bin\\ld.lld" -pie --hash-style=gnu --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out Scrt1.o crti.o crtbeginS.o boot.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtendS.o crtn.o
ld.lld: error: cannot open Scrt1.o: no such file or directory
ld.lld: error: cannot open crti.o: no such file or directory
ld.lld: error: cannot open crtbeginS.o: no such file or directory
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: cannot open crtendS.o: no such file or directory
ld.lld: error: cannot open crtn.o: no such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So it did kind of work, but i’m not sure what all these crt and -lgcc stuff is just yet.

If i change it to windows, then it does this instead which is making more sense.

PS C:\\Documents\Test> clang boot.o --target=i386-unknown-windows-coff -march=i386 -ffreestanding -v -fuse-ld=lld
clang version 16.0.0
Target: i386-unknown-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
 "C:\\Program Files\\LLVM\\bin\\lld-link" -out:a.exe -defaultlib:libcmt -defaultlib:oldnames "-libpath:C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.33.31629\\lib\\x86" "-libpath:C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.33.31629\\atlmfc\\lib\\x86" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\ucrt\\x86" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.19041.0\\um\\x86" "-libpath:C:\\Program Files\\LLVM\\lib\\clang\\16\\lib\\windows" -nologo boot.o
lld-link: error: subsystem must be defined

Adding -rtlib=compiler-rt may help. I think you may also need to install an alternative C library, like Musl.