将goto标签暴露给符号表 [英] Exposing goto labels to symbol table

查看:137
本文介绍了将goto标签暴露给符号表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将函数内的goto标签暴露给来自C / C ++的符号表

I want to know whether it is possible to expose goto label within a function to symbol table from C/C++

例如,我想使 ret 标签出现在符号表中,可以使用标准API(如dlsym())引用。

For instance, I want to make ret label of the following snippet appeared from the symbol table and can be referred using standard APIs such as dlsym().

感谢您的帮助!

#include <stdio.h>

int main () {
  void *ret_p = &&ret;
  printf("ret: %p\n", ret_p);
  goto *ret_p;

  return 1;

  ret:
  return 0;
}


推荐答案

http://stackoverflow.com/users/1918193/marc-glisse\"> Marc Glisse 的评论,这是关于使用指定标签的内联asm,我可以想出一个解决方法的问题。以下示例代码段显示了我如何解决此问题。

Thanks to Marc Glisse's comment which is about using inline asm that specifies label, I could come up with a workaround for the question. The following example code snippet shows how I solved the problem.

#include <stdio.h>

int main () {
  void *ret_p = &&ret;
  printf("ret: %p\n", ret_p);
  goto *ret_p;

  return 1;

  ret:
  asm("RET:")

  return 0;
}

这将添加一个符号表条目如下。

This will add a symbol table entry as follows.

jikk@sos15-32:~$ gcc  -Wl,--export-dynamic t.c  -ldl
jikk@sos15-32:~$ readelf -s a.out 

39: 08048620     0 FUNC    LOCAL  DEFAULT   13 __do_global_ctors_aux
40: 00000000     0 FILE    LOCAL  DEFAULT  ABS t.c
41: 0804858a     0 NOTYPE  LOCAL  DEFAULT   13 RET
42: 08048612     0 FUNC    LOCAL  DEFAULT   13 __i686.get_pc_thunk.bx
43: 08049f20     0 OBJECT  LOCAL  DEFAULT   19 __DTOR_END__

jikk@sos15-32:~$ ./a.out
ret: 0x804858a



我将进一步测试此解决方法,验证这是否产生任何意外副作用。

I'll further test this workaround the verify whether this produces any unexpected side effects.

感谢

这篇关于将goto标签暴露给符号表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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