1

still me. I am following the sample code given in a book but I got a segmentation fault error when linking extern asm file with cpp file

Could someone tell me what the issue is:


global _integer_add
; section .text i ran the code with and without "section .text" 
_integer_add:
        ; the parameters are passed  like this
        ; rdi, rsi, rdx, r8, r9
        ; a  ,  b ,  c , d
        ; I think i need to align rsp to 16 multiple
        sub rsp, 8
        ; mov a into rax
        mov rax, rdi
        ; sum a and b together
        add rax, rsi
        ; subtract from this sum c
        sub rax, rdx
        ; sum d to this quantity 
        add rax, r8
        ret 

this is the assembly file. I tried to run both with and without the instruction sub rsp, 8. (technically I found this written in the form sub 8, rsp in a github repo, tried that too without any luck). Maybe I am getting wrong the mov add instruction. But i found this thanks to a previous question

before call the stack pointer must be aligned by a multiple of 16 bytes after call pushes the return value to the stack.

This often fails by default without explicitly changing %rsp because call will store the 8 byte return address on the stack, so you usually need to do:

sub 8, rsp
call f

often this results in a segmentation fault. In case you need this too

extern "C" int integer_add(int a, int b, int c, int d);

solution

I actually found the problem. int is 32 bit so it worked using 32 bit registers (I also left out all the rsp register business):
_integer_add
        mov eax, edi
        add eax, esi
        sub eax, edx
        add eax, r8d
        ret

just in case someone else makes a rookie mistake as I did :)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • `sub rsp, 8` - okay, but now your stack pointer is misaligned relative to the return address on stack, so `ret` jumps to who-knows-where. – 500 - Internal Server Error Dec 17 '21 at 23:00
  • could you give a full answer. I would like to understand this. – not_here_to_play Dec 17 '21 at 23:04
  • 2
    On entry `rsp` points to the return address pushed on stack by the `call` that got us here, so `rsp` needs to be rewound back to that same address before the `ret` call - otherwise it will use whatever `rsp` happens to point to as its return address. --- Aside: IIRC, stack alignment only matters if your function makes calls to other functions. – 500 - Internal Server Error Dec 17 '21 at 23:06
  • ok, thank you very much for your explanation :) – not_here_to_play Dec 17 '21 at 23:07
  • Almost a duplicate of [Why am I getting a segmentation fault when moving the stack pointer?](https://stackoverflow.com/a/57224646), but that question is 32-bit code, and more importantly was also setting up EBP as a frame pointer, so the answer suggests restoring ESP by copying from EBP, rather than an add to undo it. – Peter Cordes Dec 18 '21 at 02:00

0 Answers0