LLVM Discussion Forums

Ruuning assembly code in LLVM

I have assembly code as:
.file “ex.c”
.globl main # – Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main


pushq	%rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq	%rsp, %rbp
.cfi_def_cfa_register %rbp
subq	$16, %rsp
movl	$0, -4(%rbp)
movabsq	$.L.str, %rdi
movb	$0, %al
callq	printf
xorl	%ecx, %ecx
movl	%eax, -8(%rbp)          # 4-byte Spill
movl	%ecx, %eax
addq	$16, %rsp
popq	%rbp
.cfi_def_cfa %rsp, 8

.size main, .Lfunc_end0-main
# – End function
.type .L.str,@object # @.str
.section .rodata.str1.1,“aMS”,@progbits,1
.asciz “Hello”
.size .L.str, 6

.ident	"clang version 10.0.0-4ubuntu1 "
.section	".note.GNU-stack","",@progbits
.addrsig_sym printf

When I use llvm-as -o test.o test.s I am getting error as :
llvm-as: test.s:1:2: error: expected top-level entity

How can I create object code from assembly code in LLVM?

Thank You

Hello Aruna,

llvm-as takes LLVM-assembly that is .ll file and output LLVM byte code that is .bc file.
See https://llvm.org/docs/CommandGuide/llvm-as.html#description

clang -c test.s will give an object (in .o format) file.
See https://clang.llvm.org/docs/CommandGuide/clang.html#cmdoption-c

For backend, most useful tool is llc(LLVM static compiler).
See https://llvm.org/docs/CommandGuide/llc.html#cmdoption-llc-filetype
llc -filetype=obj test.ll will give also you object code(.o file) from llvm assembly(.ll) file.

Also in any open source project, your best friend is always documentation.
See the llvm tools command guide https://llvm.org/docs/CommandGuide/ to know their uses.