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

编程入门 行业动态 更新时间:2024-10-28 14:26:31
本文介绍了64位GCC混合32位和64位指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尽管代码可以正常工作,但我对编译器似乎混合使用相同类型的32位和64位参数的决定感到困惑.具体来说,我有一个接收三个char指针的函数.查看汇编代码,将三个中的两个作为64位指针(按预期方式)传递,而将第三个本地常量(但仍然是字符串)作为32位指针传递.我看不到当第3个参数不是完全加载的64位指针时函数如何知道.显然,只要高端为0都没有关系,但我认为它并没有努力确保这一点.在此示例中,任何东西都可能在RDX的高端.我想念什么?顺便说一句,接收函数假定它是一个完整的64位指针,并在条目中包含以下代码:

Although the code works, I'm baffled by the compiler's decision to seemingly mix 32 and 64 bit parameters of the same type. Specifically, I have a function which receives three char pointers. Looking at the assembly code, two of the three are passed as 64-bit pointers (as expected), while the third, a local constant, but character string nonetheless, is being passed as a 32-bit pointer. I don't see how my function could ever know when the 3rd parameter isn't a fully loaded 64-bit pointer. Obviously it doesn't matter as long as the higher side is 0, but I don't see it making an effort to ensure that. Anything could be in the high side of RDX in this example. What am I missing? BTW, the receiving function assumes it's a full 64-bit pointer and includes this code on entry:

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? }

推荐答案

将其添加为答案,因为它包含原始问题的部分难题":

Adding this as an answer, as it contains "part of the puzzle" for the original question:

只要编译器可以[通过例如指定满足此要求的内存模型]确定.LC4在前4GB之内,它就可以执行此操作. %edx将被加载LC4地址的32位,并且高位被设置为零,因此,当edc_function()被调用时,它可以使用%rdx的完整64位,并且只要地址在该范围之内即可.较低的4GB,它将正常工作.

As long as the compiler can determine [by for example specifying a memorymodel that satisfies this] that .LC4 is within the first 4GB, it can do this. %edx will be loaded with 32 bits of the address of LC4, and upper bits set to zero, so when the edc_function() is called, it can use the full 64-bits of %rdx, and as long as the address is within the lower 4GB, it will work out fine.

更多推荐

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

本文发布于:2023-07-28 04:49:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   GCC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!