I'm trying to assemble an assembly file as followed. The executable file should output the command arguments.
.text
.globl _start
_start:
popl %ecx
vnext:
popl %ecx
test %ecx,%ecx
jz exit
movl %ecx,%ebx
xorl %edx,%edx
strlen:
movb (%ebx),%al
inc %edx
inc %ebx
test %al,%al
jnz strlen
movb $10,-1(%ebx)
movl $4, %eax
movl $1, %ebx
int $0x80
jmp vnext
exit:
movl $1, %eax
xorl %ebx, %ebx
int $0x80
When i use as and ld to assemble and link, no error occurs. But when i use gcc, errors as followed.
# gcc -m32 -no-pie arg_str.s -o arg_str
/tmp/ccXBtNYB.o: In function '_start':
(.text+0x0): multiple definition of '_start'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib32/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib32/crt1.o: In function '_start':
(.text+0x28): undefined reference to 'main'
collect2: error: ld returned 1 exit status
It seems that gcc requires main
function as the start of a program and does not allow the use of _start
. Any solution to avoid that error?