kjee
kjee

Reputation: 427

Exposing goto labels to symbol table

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

Upvotes: 4

Views: 1271

Answers (1)

kjee
kjee

Reputation: 427

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

Upvotes: 3

Related Questions