Segfault在简单的C / NASM x86_64 / C程序中打印浮点数

时间:2016-12-20 00:53:37

标签: c linux assembly nasm x86-64

  1. 在我的C代码中,我调用assembly(NASM x86_64)函数(main.c)。
  2. 在程序集中我调用C函数(printf.asm)
  3. 该函数使用printf打印float。 (c_printf.c)
  4. 我遇到了段错误。
  5. 我应该以其他方式编译/链接吗?也许我从汇编中调用C函数的方式不正确?

    我编译:

    gcc -c c_printf.c
    nasm -f elf64 -F dwarf -g printf.asm
    gcc -o main printf.o c_printf.o main.c
    

    我的代码:

    的main.c

    extern void start_asm(void);
    int main(void) {
        start_asm();
        return 0;
    }
    

    printf.asm

    global start_asm
    extern printf_float
    start_asm:
        call printf_float
        ret
    

    c_printf.c

    #include <stdio.h>
    void printf_float(void) {
        printf("%f\n", 42.0f);
    }
    

    也许valgrind输出会有帮助吗?

    ==16257== Memcheck, a memory error detector
    ==16257== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
    ==16257== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
    ==16257== Command: ./main
    ==16257== 
    --16257-- WARNING: Serious error when reading debug info
    --16257-- When reading debug info from /home/starsep/stack/main:
    --16257-- Overrun whilst parsing .debug_abbrev section(2)
    ==16257== 
    ==16257== Process terminating with default action of signal 11 (SIGSEGV)
    ==16257==  General Protection Fault
    ==16257==    at 0x4E8F824: printf (printf.c:28)
    ==16257==    by 0x40055F: printf_float (in /home/starsep/stack/main)
    ==16257==    by 0x400534: ??? (printf.asm:6)
    ==16257==    by 0x4E5A82F: (below main) (libc-start.c:291)
    ==16257== 
    ==16257== HEAP SUMMARY:
    ==16257==     in use at exit: 0 bytes in 0 blocks
    ==16257==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
    ==16257== 
    ==16257== All heap blocks were freed -- no leaks are possible
    ==16257== 
    ==16257== For counts of detected and suppressed errors, rerun with: -v
    ==16257== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    [1]    16257 segmentation fault  valgrind ./main
    

0 个答案:

没有答案
相关问题