4

with à 64 Linux system and using NASM.

I'm trying too link my ASM (hello.asm) file with a C file (main.c) and compile to a execution file.

I create a ASM file that print "Hello" with printf by using printHello function.

extern printf, exit
section .data
    format db "Hello", 10, 0
section .text
    global printHello
    printHello:
        sub rsp, 8
        mov rsi, 0x12345677
        mov rdi, format
        xor rax, rax
        call printf
        mov rdi, 0
        call exit

I create a simple main.c and call my function "printHello" to print "Hello"

#include <stdio.h>

void printHello();

int main()
{
    printHello();
}

My command for compile :

$ nasm -f elf64 hello.asm
$ gcc -c main.c
$ gcc -o executable main.o hello.o
$ ./executable

And it prints :

./executable: Symbol `printf' causes overflow in R_X86_64_PC32 relocation
./executable: Symbol `exit' causes overflow in R_X86_64_PC32 relocation
[1]    6011 segmentation fault  ./executable

I'm already learning ASM. Is the problem come from my command or my code ?

Benjamin Sx
  • 653
  • 1
  • 7
  • 18
  • 1
    What version of linux what version of gcc, what is the output of `file executable`? PS: in the meantime you can probably change `printf` to `printf wrt ..plt` and similarly for `exit`. – Jester Mar 01 '18 at 18:53
  • The out put is executable: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f1ef9d13c6162ebb2c4085402decc5735083b483, not stripped I use : gcc (Debian 6.4.0-2) 6.4.0 20170724 And distro is : Linux benjamin-PC 4.14.0-deepin2-amd64 #1 SMP PREEMPT Deepin 4.14.12-2 (2018-01-06) x86_64 GNU/Linux – Benjamin Sx Mar 01 '18 at 19:00
  • 1
    The problem comes from your linux being set up by default to produce PIE/PIC 64b code, which requires to write the assembly interoperating with C in a bit more complex way. If you are just learning assembly, maybe reconsider to switch PIE/PIC off, and produce position-dependent binaries, maybe even statically linked at first, to avoid the complexities of PIC and dynamic linking with .so libs. Or just study harder this topic, how to write these in expected way, it's a bit of added complexity, but it's practical current stuff being actually used, so it's not like learning DOS interrupts... – Ped7g Mar 01 '18 at 19:08
  • 2
    Yeah that means you are on a system with PIE as default. You could try `gcc -no-pie` too. That said, on my system I get a linker error, it doesn't produce an executable. – Jester Mar 01 '18 at 19:09
  • 1
    You can also check my answer on codereview where I was fighting myself to get correct PIE binary, calling Clib functions from asm, I kept there various stages (including the not completely correct ones) plus comments what changes I had to do: https://codereview.stackexchange.com/questions/181953/adding-integers-together/181964#181964 – Ped7g Mar 01 '18 at 19:13

1 Answers1

2

I resolved the problem by using your @Jester solution :

gcc -no-pie -o executable main.o hello.o

and thanks Ped7g for explanation.

Benjamin Sx
  • 653
  • 1
  • 7
  • 18