Why does mov fail to set dynamic section sizes when used on a function using GCC

32 Views Asked by At

I'm working on implementing threads in x86_64 Intel assembly, and when using GCC, I receive the following error on compilation, even when I use the -fPIE flag.

/usr/bin/ld: /tmp/cc7xDzsl.o: relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status

The assembly compiles without errors when I remove the following line, where f is a label.

mov rdi, f

I've attempted a number of things I've seen on here, such as mov rdi, [f] and mov rdi, offset f, but I haven't been able to work out an answer. I'm not massively familiar with GCC or x86, so I'm probably missing something. I'll put the code below here, if you have any ideas. Thanks!

.intel_syntax noprefix 
.globl main 
.section .rodata 
.text
f:                 # Exits thread
    mov rdi, 0
    mov rax, 60
    syscall

main: 
    call create_thread_stack
    pop rsi                    # --
    sub rsi, 8                 # | Put function pointer onto
    mov rdi, f                 # | new stack
    push rdi                   # --
    pop qword ptr [rsi]        # Put function pointer onto new stack
    mov rdi, -2147479808       # Flags
    mov rax, 56                # Sys_clone
    syscall
    ret

create_thread_stack:    # Mmaps stack for cloned process
    mov rdi, 0
    mov rsi, 4194304    # Size of stack
    mov rdx, 3          
    mov r10, 290
    mov r9, 0
    mov r8, -1
    mov rax, 9          # Sys_mmap
    syscall
    add rax, 4194304
    push rax            # Return base pointer of new stack
    ret
0

There are 0 best solutions below