You can use BFD to get runtime addresses from symbol names.
#include <stdio.h>
#include <stdlib.h>
#include <bfd.h>
#include <string.h>
void func1(int n)
{
printf("%d\n", n);
}
void * addr_from_name(char * filename, char * symname)
{
bfd * ibfd;
asymbol ** symtab;
symbol_info info;
void * symaddress = NULL;
long size;
long syms;
unsigned int i;
bfd_init();
ibfd = bfd_openr(filename, NULL);
bfd_check_format(ibfd, bfd_object);
size = bfd_get_symtab_upper_bound(ibfd);
symtab = malloc(size);
syms = bfd_canonicalize_symtab(ibfd, symtab);
for(i = 0; i < syms; i++) {
if(strcmp(symtab[i]->name, symname) == 0) {
bfd_symbol_info(symtab[i], &info);
symaddress = (void *)info.value;
}
}
bfd_close(ibfd);
return symaddress;
}
int main(void)
{
void (*func)(int) = addr_from_name("/homes/mk08/Desktop/lala", "func1");
printf("%p\n", func);
func(5);
return EXIT_SUCCESS;
}
filename, . , / lala, :
gcc -Wall -std=gnu99 lala.c -o lala -lbfd && ./lala
:
0x400814
5
, . , . . .