8


I am trying to obtain the address (in hex) of function exit() provided in libc, but I am not sure where and how to find it.
Anyone knows the way to find it please share some idea. Thank you!

Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
  • 1
    All the answers are good, but you should realize that `exit(3)` is a standard library function invoking the `_exit(2)` syscall which is not really a function but a system call (you could invoke it with assembler code without any ordinary function calls, even if the `_exit` function from libc is wrapping the syscall as a function callable by C)... The real work of system calls is done inside the kernel. – Basile Starynkevitch Mar 08 '13 at 20:38

5 Answers5

9

If you need the address of the exit function already present in your process, see answers by Grijesh and others. But if you need to resolve the libc exit function by name, for example because libc's exit has been shadowed by another library, you can obtain it with dlsym:

#define _GNU_SOURCE     /* for RTLD_NEXT */
#include <dlfcn.h>
/* ... */
void (*exit_addr)(int) = dlsym(RTLD_NEXT, "exit");

For dlsym to resolve, you'll need to link with -ldl.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • 2
    Thanks to appreciate my answer and thanks for your good technical answer :) – Grijesh Chauhan Mar 08 '13 at 20:02
  • This was really helpful. To add a little bit of info that had me stuck for a few minutes, the command to link with ldl would be for example `gcc -o getptr getptr.c -ldl`. The -ldl comes after the gcc stuff. – Rstevoa Jun 03 '14 at 14:15
6

I think this will work:

printf("%p", (void*)exit);

IEEE Std 1003.1, 2004 Edition:

"%p" The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printable characters, in an implementation-defined manner.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
5

The address of any function is just its name (without the parentheses). You'll need #include <stdlib.h> as well. To set an initalised pointer:

void (*p)(int) = exit;
teppic
  • 8,039
  • 2
  • 24
  • 37
3

You can use gdb as follow:

gdb ./yourprogram
break main
run
print exit
$1 = {<text variable, no debug info>} 0xb7e4b7f0 <exit>
here is exit() address----------------^
AK_
  • 1,879
  • 4
  • 21
  • 30
1
printf("%p", exit);

You will have to include stdio.h for printf and stdlib.h for exit. This creates a function pointer to exit and prints it.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62