gpt4 book ai didi

c - 程序集-从字符数组中获取符号标签?

转载 作者:行者123 更新时间:2023-11-30 17:01:15 28 4
gpt4 key购买 nike

是的,我正在努力实现类似的目标

__asm__(jmp label;); 其中 label 应替换为内存中保存的字符串值(结构体的字段)。

有没有办法做到这一点(或类似的方法可以让我跳转到运行时确定的位置)?

更新@Jester 建议的 dlsym 看起来很有希望,我现在正在尝试了解如何使用它。

如果我尝试以下操作(gcc 标志 -Wall -std=c99 -m32 -ldl)

#include <stdlib.h> //malloc
#include <stdio.h> //printf
#include <stdint.h> //uint32_t
#include <string.h> //strlen,strdup,...
#include <dlfcn.h> //dlsym

void test(){printf("Hello World\n");}
int main(int argc, char* argv[]){
//this works
__asm__(
"call test;"
);

//I'd like this to have the same effect
char *label = "test";
void *handle = dlopen(NULL,RTLD_NOW);
uint32_t loc = (uint32_t)dlsym(handle,label);
__asm__(
"call %0;"
:
:"rm"(loc)
);

}

我明白了

Hello World

Segmentation fault (core dumped)

最佳答案

asm 应该类似于 "call *%0"::"rm"(loc) : "eax", "ecx", "edx", "memory" 并且您需要添加 -rdynamic 编译器标志,否则你会得到一个 NULL 指针;) – Jester

当然,从内联汇编中调用函数很难正确,特别是对于 64 位而言。此外,编译器/优化器无法看到的调用可能会导致无效的链接时过程间优化。

使用 C 函数指针更容易、更好

关于c - 程序集-从字符数组中获取符号标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169050/

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