gpt4 book ai didi

c - 如何在c中获取asm函数的地址

转载 作者:太空狗 更新时间:2023-10-29 15:32:14 25 4
gpt4 key购买 nike

我有一个 asm 文件(我正在使用 gas)和一个 c 文件。我的 asm 文件包含一个函数,类似这样

.global myfunc
.type myfunc, @function

myfunc:
pusha
.
.
.

现在我想从我的c文件中获取标签myfunc的地址,所以我写了

extern uintptr_t _myfunc asm("myfunc");

我的程序链接没有问题,但是当我执行我的程序时,变量 _myfunc 不包含正确的地址。

编辑 1

知道这段代码是裸机程序的一部分可能很有用

编辑2

使用函数原型(prototype)解决了问题

void function() asm("myfunc");

uintptr_t _myfunc = (uintptr_t)&myfunc;

最佳答案

您应该能够使用在 .global 中使用的相同符号在 C 中声明您的 asm 函数声明:

extern void myfunc(void);

符号由链接器解析。只要从您的 asm 文件创建的目标文件导出符号 myfunc ,您的 C 源代码中的目标文件可能包含对它的 undefined reference 。

根据特定的 ABI 或目标文件格式(a.out 与 ELF),您可能需要使用 _myfunc而不是 myfuncextern声明。

关于c - 如何在c中获取asm函数的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32231767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com