用ASM编译ASM和C以进行调试 [英] Compile ASM and C with ASM for debugging

查看:1068
本文介绍了用ASM编译ASM和C以进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要在CMake中编译的小文件,以便使用CLion和GDB对其进行调试

I have two small files that I want to compile in CMake for debugging it with CLion and GDB

main.c

int my_strlen(char *);

int main()
{
    printf("Test: %d\n", my_strlen("Hello"));
}

我的ASM文件中有一个my_strlen文件

and I have my ASM file that have a my_strlen file

    [BITS 64]

    global my_strlen
    section .text

my_strlen:
    push rbp
    mov rbp, rsp
    xor rcx, rcx

loop:
    cmp BYTE [rdi + rcx], 0
    jz end
    inc rcx
    jmp loop

end:
    mov rax, rcx
    mov rsp, rbp
    leave
    ret

我正在尝试使用CMakeList.txt进行编译,我添加了set_source_files_properties,但仍然无法正常工作

I'm trying to compile with a CMakeList.txt I add set_source_files_properties but it still doesn't work

cmake_minimum_required(VERSION 3.9)
project(ASM)

set(CMAKE_CXX_STANDARD 11)
set_source_files_properties(src/strlen.asm PROPERTIES COMPILE_FLAGS "-x assembler-with-c")

add_executable(main
        src/strlen.asm
        tests/src/main.c)

有人知道在C项目中添加ASM文件并使用CMakeList.txt进行编译的好命令吗?

Someone knows a good command to add ASM file in a C project and compile with a CMakeList.txt ?

推荐答案

您可能必须使用enable_language(ASM_NASM)在CMAKE中启用NASM支持,然后设置适当的组装选项:

You'll probably have to enable NASM support in CMAKE with enable_language(ASM_NASM)and then set appropriate assembly options:

cmake_minimum_required(VERSION 3.9)
project(ASM)
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
    <FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")

set_source_files_properties(src/strlen.asm PROPERTIES COMPILE_FLAGS "-g -Fdwarf")

set(CMAKE_CXX_STANDARD 11)

add_executable(main
        src/strlen.asm
        tests/src/main.c)

由于您的代码似乎是64位目标,因此我实际上将-f elf64作为命令行选项传递给NASM.我将格式类型放在其自己的环境变量CMAKE_ASM_NASM_OBJECT_FORMAT中.要在NASM中启用调试信息,可以使用-g -Fdwarf.

Since your code seems to be a 64-bit target I essentially pass -f elf64 as a command line option to NASM. I place the format type in its own environment variable CMAKE_ASM_NASM_OBJECT_FORMAT. To enable debug information in NASM you can use the -g -Fdwarf.

如果执行调试和发布"构建,则可以检查构建类型,并使用以下类似的方法相应地设置标志:

If doing Debug and Release builds you could check for the build type and set the flags accordingly with something like:

cmake_minimum_required(VERSION 3.9)
project(ASM)
enable_language(ASM_NASM)

set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
    <FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_ASM_NASM_FLAGS "${ASM_NASM_FLAGS} -g -Fdwarf")
else()
    set(CMAKE_ASM_NASM_FLAGS "${ASM_NASM_FLAGS}")
endif()

set(CMAKE_CXX_STANDARD 11)

add_executable(main
        src/strlen.asm
        tests/src/main.c)

这篇关于用ASM编译ASM和C以进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆