Exposing goto labels to symbol table

回眸只為那壹抹淺笑 提交于 2019-12-23 19:42:31

问题


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

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().

Thanks for your help in advance!

#include <stdio.h>

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

  return 1;

  ret:
  return 0;
}

回答1:


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.

Thanks



来源:https://stackoverflow.com/questions/14268325/exposing-goto-labels-to-symbol-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!