LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 01-08-2020, 03:27 AM   #1
zxuiji
LQ Newbie
 
Registered: Jan 2020
Posts: 2

Rep: Reputation: Disabled
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]);
 
Old 01-09-2020, 12:36 PM   #2
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
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.

Last edited by Geist; 01-09-2020 at 01:34 PM.
 
Old 01-12-2020, 06:44 PM   #3
zxuiji
LQ Newbie
 
Registered: Jan 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
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

The C File
https://drive.google.com/file/d/1d7H...ew?usp=sharing
The Lua file
https://drive.google.com/file/d/1QxJ...ew?usp=sharing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Google I/O Android News: Location, Location, Location (Plus Cloud Messaging and Bluetooth) LXer Syndicated Linux News 0 06-05-2013 01:00 PM
GLUT location true_atlantis Fedora 1 08-28-2005 06:34 AM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM
location, location, location! mermxx LQ Suggestions & Feedback 9 09-25-2004 03:08 AM
kppp dialing into a NT Ras callback server MrBiggZ Linux - Software 1 02-10-2004 06:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration