0

I have a C program linked into my NASM program to call printf. The C progam has two functions, one to print integer values and the other to print float values:

int Test_Print_2(int64_t value_passed_in)
{
    printf ("value: %lu\n", value_passed_in);
return 0;
}

int Test_Print_2F(double value_passed_in)
{
    printf ("Inside Test_Print - Float ");
    printf ("q[0]: %f\n", value_passed_in);
    //printf ("q[0]: %4.4lf\n", value_passed_in);
return 0;
}

The first program (Test_Print_2) works with no problem, but the second program (Test_Print_2F) throws a segfault. It doesn’t even print the line "Inside Test_Print - Float " If I comment out the print line then it does print the line "Inside Test_Print - Float " – so something is wrong with my format for printing doubles.

To print as integer, I call it from NASM like this:

movsd xmm0,[rdi] ; the values in the array pointed to by rdi are doubles
cvttsd2si rax,xmm0 ; convert to integer
%include "/opt/P01_SH/_Include_Utilities/ABI_Stack_Push.asm"
mov rdi,rax
call [rel Test_Print_2 wrt ..got]
%include "/opt/P01_SH/_Include_Utilities/ABI_Stack_Pop.asm"

That works and prints just the integer part of the floating point number. However, when I call it from NASM to print as a double like this:

movsd xmm0,[rdi]
%include "/opt/P01_SH/_Include_Utilities/ABI_Stack_Push.asm"
mov rax,1
call [rel Test_Print_2F wrt ..got]
%include "/opt/P01_SH/_Include_Utilities/ABI_Stack_Pop.asm"

it segfaults without printing anything. Both programs are linked in from the object file (I often link C object files into NASM).

Thanks for any help on this.

RTC222
  • 2,025
  • 1
  • 20
  • 53
  • 1
    You likely misaligned the stack which will blow up when `printf` tries to use aligned instructions to manipulate the floating point values. – Jester May 03 '22 at 21:58
  • Right before the call I use an include file that pushes rdi, rsi, rdx, rcx, r8 and r9. That's 48 bytes. Did that cause misalignment? If I sub another 16 bytes (total 64) will that solve it? – RTC222 May 03 '22 at 22:03
  • I answered my own question. What I described in the previous comment worked. Thanks much. – RTC222 May 03 '22 at 22:10
  • 1
    If moving RSP by even multiple of 8 solved it, this definitely isn't a [mcve] of your problem, and it's not a duplicate of [Printing floating point numbers from x86-64 seems to require %rbp to be saved](https://stackoverflow.com/q/16097173) . But unless you had some unrelated bug or stepping on stack space more likely you changed the number of pushes and it's an *odd* multiple of 8, so it is a duplicate. And you're on a build of glibc where dumping the XMM args for AL!=0 is the only thing in printf that depends on alignment of RSP for correctness. – Peter Cordes May 04 '22 at 01:00
  • Related: [glibc scanf Segmentation faults when called from a function that doesn't align RSP](https://stackoverflow.com/q/51070716) (recent gcc happens to compile glibc scanf in a way that depends on the ABI rules for RSP alignment outside of the variadic function dumping XMMs to stack mem). – Peter Cordes May 04 '22 at 01:05

0 Answers0