Copying a callback from memory into a new location and passing that to glut
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Copying a callback from memory into a new location and passing that to glut
So I managed to get part way there by creating my own wrapper for mmap/unmap and associated a wrapper function for that one to assigned parts of the assigned memory to each instance of the callback I need to make, unfortunately whenever glut then attempts to call that function I get a segfault, since the callbacks worked fine before I started attempting to duplicate them I can only assume I'm not copying the right memory, I would really appreciate some help on this, here's the method I'm currently using to acquire the pointer to the bytes to copy:
Code:
#if __INTPTR_WIDTH__ == 32
#define ELF_T( NAME ) Elf32_##NAME
#elif __INTPTR_WIDTH__ == 64
#define ELF_T( NAME ) Elf64_##NAME
#else
#define ELF_T( NAME ) Elf_##NAME
#endif
//#define MAKE_CB_COPY
void alloc_cb_copies( char *name ) {
#ifdef __ELF__
size_t prev = BUFSIZ, need = prev, info = 0, symtabi = 0,
symtabs_used = 0, strtabs_used = 0, strtabs_upto = 10;
std_data data = {0};
uchar *buff = NULL;
char *text;
if ( (errno = alloc_foo_data( &data, need )) != EXIT_SUCCESS ) {
exit(errno);
return;
}
buff = data.data;
FILE *file = fopen(name,"r");
if ( !file ) {
(void)alloc_std_data(&data,0);
exit(errno);
return;
}
(void)fread( buff, prev, 1, file );
ELF_T(Ehdr) *elf_header = (ELF_T(Ehdr) *)buff;
ELF_T(Shdr) *sec_headers = NULL, *sec_header = NULL;
ELF_T(Sym) *syms = NULL, *sym = NULL;
ELF_T(Half) sectioni, symi, sym_count = 0,
symtabs[10] = {0}, strtabs[10] = {0};
foo_cb_id_t foo_cb_id;
sec_headers = (ELF_T(Shdr)*)elf_header->e_shoff;
if ( elf_header->e_phoff > elf_header->e_shoff )
need = elf_header->e_phoff +
(elf_header->e_phnum * sizeof(ELF_T(Phdr)));
else
need = elf_header->e_shoff +
(elf_header->e_shnum * sizeof(ELF_T(Shdr)));
if ( elf_header->e_shstrndx + BUFSIZ > need )
need = elf_header->e_shstrndx + BUFSIZ;
if ( elf_header->e_entry > need )
need = elf_header->e_entry;
if ( need > prev ) {
if ( (errno = alloc_foo_data( &data, need )) != EXIT_SUCCESS )
goto failed;
buff = data.data;
(void)fseek( file, 0, SEEK_SET );
(void)fread( buff, need, 1, file );
prev = need;
elf_header = (ELF_T(Ehdr)*)buff;
sec_headers = (ELF_T(Shdr)*)&(buff[elf_header->e_shoff]);
}
if ( sec_headers ) {
for (
sectioni = 0;
sectioni < elf_header->e_shnum;
++sectioni
)
{
sec_header = &(sec_headers[sectioni]);
if ( (sec_header->sh_type == SHT_STRTAB ||
sectioni == elf_header->e_shstrndx)
&& strtabs_used < strtabs_upto ) {
strtabs[strtabs_used++] = sectioni;
info = sec_header->sh_offset + sec_header->sh_size;
if ( info > need ) need = info;
}
if ( (sec_header->sh_type == SHT_SYMTAB
|| sec_header->sh_type == SHT_DYNSYM)
&& symtabs_used < strtabs_upto ) {
symtabs[symtabs_used++] = sectioni;
info = sec_header->sh_offset + sec_header->sh_size;
if ( info > need ) need = info;
}
}
if ( symtabs_used == 0 ) {
(void)puts("Section '.symtab' missing!");
goto failed;
}
if ( need > prev ) {
if ( (errno = alloc_foo_data( &data, need )) != EXIT_SUCCESS )
goto failed;
buff = data.data;
(void)fseek( file, 0, SEEK_SET );
(void)fread( buff, need, 1, file );
prev = need;
elf_header = (ELF_T(Ehdr)*)buff;
sec_headers = (ELF_T(Shdr)*)&(buff[elf_header->e_shoff]);
}
}
if ( symtabs_used ) {
need = 0;
for ( symtabi = 0; symtabi < symtabs_used; ++symtabi )
{
sec_header = &(sec_headers[symtabs[symtabi]]);
syms = (ELF_T(Sym)*)&(buff[sec_header->sh_offset]);
sym_count = sec_header->sh_size / sizeof(ELF_T(Sym));
sec_header = &(sec_headers[sec_header->sh_link]);
text = (char*)&(buff[sec_header->sh_offset]);
for ( symi = 0; symi < sym_count; ++symi )
{
sym = &(syms[symi]);
name = &(text[sym->st_name]);
for (
foo_cb_id = 0;
foo_cb_id < foo_cb_id_count;
++foo_cb_id
)
{
if ( strcmp(name,foo_callbacks[foo_cb_id].name) ==0) {
#ifdef MAKE_CB_COPY
foo_callbacks[foo_cb_id].cb = (void*)need;
#endif
need += foo_callbacks[foo_cb_id].size = sym->st_size;
#ifdef MAKE_CB_COPY
if ( (errno = alloc_foo_data( &foo_cb_copies, need )) != EXIT_SUCCESS ) {
exit(errno);
return;
}
(void)fseek( file, sym->st_value + elf_header->e_entry, SEEK_SET );
(void)fread( (uchar*)foo_cb_copies.data + need,
sym->st_size, 1, file );
#endif
}
}
}
}
}
#ifdef MAKE_CB_COPY
for (
foo_cb_id = 0;
foo_cb_id < foo_cb_id_count;
++foo_cb_id
)
{
foo_callbacks[foo_cb_id].cb =
&(((uchar*)(foo_cb_copies.data))
[(uintptr_t)(foo_callbacks[foo_cb_id].cb)]);
}
#endif
data.data = buff;
data.size = prev;
(void)alloc_foo_data(&data,0);
if ( file ) (void)fclose(file);
return;
failed:
data.data = buff;
data.size = prev;
(void)alloc_foo_data(&data,0);
if ( file ) (void)fclose(file);
exit(errno);
#else
(void)puts("Must be compiled as an elf!");
exit(EXIT_FAILURE);
#endif
}
int main( int argc, char *argv[] ) {
int w;
std_data data;
foo_cb_info_t callbacks[foo_cb_id_count] = {
FOO_CB_INFO(foo_cb_close),
FOO_CB_INFO(foo_cb_display),
FOO_CB_INFO(foo_cb_idle),
FOO_CB_INFO(foo_cb_keyboard),
FOO_CB_INFO(foo_cb_reshape),
FOO_CB_INFO(foo_cb_special),
};
g_argc = argc;
g_argv = argv;
atexit( foo_atexit );
memcpy( foo_callbacks, callbacks, sizeof(foo_cb_info_t) * foo_cb_id_count );
alloc_cb_copies(argv[0]);
This, and that might not be a surprise at all to you, doesn't even begin to compile (so many missing things), but if this is code written after the year 1500...then you might want to refactor a bunch of things anyway.
That said, I'm giving it a look over.
Edit:
Yeah there is a lot of magic going on with allocations and unknown functions, but maybe I just am not skilled enough to help, which is completely possible.
Yeah wasn't expecting that to compile since it only a piece of my file, anyway I think I made some changes since then but I'm still struggling to get it to execute. For now besides what I've shown you already the functions of interest from the file are alloc_foo_data and alloc_foo_window and any of the actual callbacks referenced in the code I posted, I know alloc_foo_data still has a bug somewhere but that only pops up when reallocating data which I've yet to start doing with the windows and their callbacks
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.