I copied a sample assembly code from wiki:
.section .data
s0 : .asciz "Processor Brand String: %.48s\n"
err : .asciz "Feature unsupported.\n"
.section .text
.global main
.type main,@function
.align 32
main:
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp
pushq %rbx
movl $0x80000000, %eax
cpuid
cmpl $0x80000004, %eax
jl error
movl $0x80000002, %esi
movq %rsp, %rdi
......
......
jmp end
.align 16
error:
movq $err, %rdi
xorb %al, %al
call printf
.align 16
end:
popq %rbx
movq %rbp, %rsp
popq %rbp
xorl %eax, %eax
ret
I replaced main with _start and add syscall at the end, then built it successfully with "as" and "ld". But I thought it should be compiled directly with GCC somehow. So what do I need to do?
gcc -no-pie main.s
for this source which defines a main
, just like a .c
that defines a main
.
gcc -no-pie -nostartfiles start.s
if you define _start
(the ELF entry point) yourself. (Since you're on Linux, glibc can initialize itself via dynamic linker hooks, even if your _start
doesn't call the glibc init functions.)
To build a static binary without any libraries, gcc -nostdlib -static start.s
.
User contributions licensed under CC BY-SA 3.0