c - 64 位 GCC 混合 32 位和 64 位指针

标签 c gcc x86-64 gnu-assembler

虽然代码有效,但我对编译器看似混合相同类型的 32 位和 64 位参数的决定感到困惑。具体来说,我有一个接收三个 char 指针的函数。查看汇编代码,三个中的两个作为 64 位指针传递(正如预期的那样),而第三个是局部常量,但仍然是字符串,作为 32 位指针传递。我不明白我的函数怎么知道第三个参数何时不是完全加载的 64 位指针。显然,只要较高的一侧为 0 就没有关系,但我认为它没有努力确保这一点。在此示例中,任何东西都可能位于 RDX 的高端。我错过了什么?顺便说一句,接收函数假定它是一个完整的 64 位指针,并在入口处包含以下代码:

     movq    %rdx, -24(%rbp)

这是有问题的代码:

.LC4
    .string "My Silly String"

    .text
    .globl funky_funk
    .type  funky_funk, @function
    funky_funk:
        pushq     %rbp
            movq      %rsp, %rbp
            pushq     %rbx
            subq      $16, %rsp
            movq      %rdi, -16(%rbp)          ;char *dst 64-bit
            movl      %esi, -20(%rbp)          ;int len, 32 bits OK

            movl      $.LC4, %edx              ;<<<<---- why is it not RDX?

            movl      -20(%rbp), %ecx          ;int len 32-bits OK
            movq      -16(%rbp), %rbx          ;char *dst 64-bit
            movq      -16(%rbp), %rax          ;char *dst 64-bit
            movq      %rbx, %rsi               ;char *dst 64-bit
            movq      %rax, %rdi               ;char *dst 64-bit
            call      edc_function


    void funky_funk(char *dst, int len)
    {                                             //how will function know when 
         edc_function(dst, dst, STRING_LC4, len); //a str passed in 3rd parm
    }                                             //is 32-bit ptr vs 64-bit ptr?

    void edc_function(char *dst, char *src, char *key, int len)
    {
         //so, is key a 32-bit ptr? or is key a 64-bit ptr?
    }

最佳答案

在寄存器中加载 32 位值时,该值会被零扩展。您可能在编译器知道代码位于低 32 位可寻址内存中的模式下工作。

GCC 有多种 x64 内存模型,其中两种具有该属性。来自 GCC 文档:

`-mcmodel=small'
     Generate code for the small code model: the program and its
     symbols must be linked in the lower 2 GB of the address space.
     Pointers are 64 bits.  Programs can be statically or dynamically
     linked.  This is the default code model.
`-mcmodel=medium'
     Generate code for the medium model: The program is linked in the
     lower 2 GB of the address space.  Small symbols are also placed
     there.  Symbols with sizes larger than `-mlarge-data-threshold'
     are put into large data or bss sections and can be located above
     2GB.  Programs can be statically or dynamically linked.

(其他的都是kernel,类似small但是在upper/negative 2GB of 地址空间大,没有限制)。

关于c - 64 位 GCC 混合 32 位和 64 位指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550626/

相关文章:

c++ - Mac OS X 10.9.5 上的 HealPix C++ 链接错误

C 变量默认初始化

c - 如何在 C 中获取 shell 命令 [通过 system() 运行] 的退出状态?

c++ - CodeBlocks 没有构建我的项目

php - 我们可以在几秒钟后强制退出 C 程序的执行吗

c - Gnu 作为后缀或操作数错误

c - 有效地找到大数组中的最低有效位?

assembly - 使用 NASM 在 64 位模式下不支持 pop 指令?

c - 无法正确读取 .bmp 头文件

c - 使用 GCC 编译时出现汇编错误