GP32 Elf Guru


pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
45
Location
New Zealand
Website
www.projectitis.com
Hi all,

I am trying to compile a very simple ELF (thats the fxe file format, minus some gp32 specific stuff) and I would like to load it into memory from within a running app, find the address of a function within the code, and then call that function.

The ELF code will have no dependencies (or whatever they are called) and the function will always have the same name and always be the "entry point".

The ELF code will have no globals, and any shared functions it requires (such as malloc/free) will be passed into the function as a struct of function pointers.

Does anyone have a simple example of loading an ELF into memory and/or perhaps locating the entry point or any functions within the ELF?

On the flipside, does anyone know the best way (makefile) to compile my ELF to achieve this?
 
There's a library that does what you ask (I think it comes with GCC), but personally, I would just use a binary file as it's much easier - just have the entry point being the beginning of the file, and then can simply load it and run. Lookup 'objcopy' for copying elf files to binary files.
 
Thanks Squidge. A library would be great, but the dlopen suite of functions isn't available to my devKitARM release (dlfcn.h not found).

Any idea what I would do? Try to compile these in I guess?

EDIT: Found this ELF loader for palm, but don't know how compatible/how much work it would be:
http://www.sealiesoftware.com/peal/
 
Hi all,

I am trying to compile a very simple ELF (thats the fxe file format, minus some gp32 specific stuff) and I would like to load it into memory from within a running app, find the address of a function within the code, and then call that function.

The ELF code will have no dependencies (or whatever they are called) and the function will always have the same name and always be the "entry point".

The ELF code will have no globals, and any shared functions it requires (such as malloc/free) will be passed into the function as a struct of function pointers.

Does anyone have a simple example of loading an ELF into memory and/or perhaps locating the entry point or any functions within the ELF?

On the flipside, does anyone know the best way (makefile) to compile my ELF to achieve this?


You are searching for libelf.a

All function in it can handle your problems.

btw, its very easy, finding out the entry point of your code in an .elf file.

Use:
arm-elf-objdump font.elf -S | grep .text
Disassembly of section .text:
0c000000 <.text>:
Your main code, begins at, 0x0c000000.

cu,
 
Last edited by a moderator:
Hi all,

I am trying to compile a very simple ELF (thats the fxe file format, minus some gp32 specific stuff) and I would like to load it into memory from within a running app, find the address of a function within the code, and then call that function.

The ELF code will have no dependencies (or whatever they are called) and the function will always have the same name and always be the "entry point".

The ELF code will have no globals, and any shared functions it requires (such as malloc/free) will be passed into the function as a struct of function pointers.

Does anyone have a simple example of loading an ELF into memory and/or perhaps locating the entry point or any functions within the ELF?

On the flipside, does anyone know the best way (makefile) to compile my ELF to achieve this?

You are searching for libelf.a

All function in it can handle your problems.

btw, its very easy, finding out the entry point of your code in an .elf file.

Use:
arm-elf-objdump font.elf -S | grep .text
Disassembly of section .text:
0c000000 <.text>:
Your main code, begins at, 0x0c000000.

cu,

for a true hardcore try out the libbfd.a <_<
 
Last edited by a moderator:
Sweet! I compiled it under Mr.Mirkos SDK in the src.addon folder. I modified the makefile from one of the other src.addon's (zlib). Just one problem to fix and it compiled with no errors!

Can someone let me know hether I have done the right thing here (they were all undefined to start with):
Code:
/* Define to `<elf.h>' or `<sys/elf.h>' if one of them is present */
#undef __LIBELF_HEADER_ELF_H

/* Define if Elf32_Dyn is declared in <link.h> */
#undef __LIBELF_NEED_LINK_H

/* Define if you want 64-bit support (and your system supports it) */
#undef __LIBELF64

/* Define if you want 64-bit support, and are running IRIX */
#undef __LIBELF64_IRIX

/* Define if you want 64-bit support, and are running Linux */
#undef __LIBELF64_LINUX

/* Define if you want symbol versioning (and your system supports it) */
#undef __LIBELF_SYMBOL_VERSIONS

/* Define to a 64-bit signed integer type if one exists */
#undef __libelf_i64_t

/* Define to a 64-bit unsigned integer type if one exists */
#undef __libelf_u64_t

/* Define to a 32-bit signed integer type if one exists */
// #undef __libelf_i32_t
#define __libelf_i32_t long

/* Define to a 32-bit unsigned integer type if one exists */
//#undef __libelf_u32_t
#define __libelf_u32_t unsigned long

/* Define to a 16-bit signed integer type if one exists */
//#undef __libelf_i16_t
#define __libelf_i16_t short

/* Define to a 16-bit unsigned integer type if one exists */
//#undef __libelf_u16_t
#define __libelf_u16_t unsigned short
 
So does anybody know how to do anything usefull using libelf? i am having real problems finding examples!

Something very usefull would be loading a relocateable ELF file, resolving the symbol table and then locating a function address :D :D :D

All I can find so far is:
http://developers.sun.com/solaris/articles/elf.html
Which shows how to check that ths symbol table exists and then print it.

oooh, wait, just found some manual pages:
http://ou800doc.caldera.com/en/man/html.3elf/CONTENTS.html

But any working examples of loading/executing an elf file woukd be great!
 
So does anybody know how to do anything usefull using libelf? i am having real problems finding examples!

Something very usefull would be loading a relocateable ELF file, resolving the symbol table and then locating a function address :D :D :D

All I can find so far is:
http://developers.sun.com/solaris/articles/elf.html
Which shows how to check that ths symbol table exists and then print it.

oooh, wait, just found some manual pages:
http://ou800doc.caldera.com/en/man/html.3elf/CONTENTS.html

But any working examples of loading/executing an elf file woukd be great!

Handling of an elf file is not that hard, you can locate the section of .text ,
it is givven by an offset from the file beginning.
You then must copy this .text segement to the right RAM address.
And then execute .text at the copyied RAM.
Also dont forget to copy the .data segment.
All needed information, there to find .text .data, are in the ELF header. I wrote an
ELF analysator programm to load the .text segemnt to ram, and then dissassemle it. ( mips asm )

cu
btw, i was not using libelf, course the elf header is very easy and small, no need for an helping lib.

btw2, all needed headers are in your first link...
 
Last edited by a moderator:
I have compiled a small test elf program (hello.elf) that I am attempting to test on. A couple of issues -

1) What is the process to resolve unlinked symbols? If the hello.elf program uses (for example) malloc, and malloc is unresolved, how do i resolve this symbol? (and all the others).

2) On the flipside, when I compile hello.elf, how do I specify the functions NOT to include in the code, but to add as unresolved symbols instead? (such as malloc, free, and all other shared functions).

3) If i compile my code with -fPIC, does it matter which part of memory I copy the .text chunk to?

My makefile is like this:
Code:
CC = arm-elf-gcc
LD = arm-elf-gcc
AS = arm-elf-as
AR = arm-elf-ar

PRG  = hello
OBJS = hello.o

LIBS      = -L../lib -lmirkoSDK -laddon -lm -lelf
CRT0      = ../lib/crt0.S
LNKSCRIPT = ../lib/lnkscript
INCLUDES  = -I../include
CFLAGS    = $(INCLUDES) -O3 -s -Wall -mtune=arm9tdmi -fPIC

all:	$(OBJS)
	$(CC) -c -o crt0.o $(CRT0)
	$(LD) -nostartfiles -s -Wall -Wl,-Map,Test.map  -T $(LNKSCRIPT) crt0.o -o $(PRG).elf $(OBJS) $(LIBS)

clean:
	rm -f *.o *~ Test.map *.bin
 
I have compiled a small test elf program (hello.elf) that I am attempting to test on. A couple of issues -

1) What is the process to resolve unlinked symbols? If the hello.elf program uses (for example) malloc, and malloc is unresolved, how do i resolve this symbol? (and all the others).

2) On the flipside, when I compile hello.elf, how do I specify the functions NOT to include in the code, but to add as unresolved symbols instead? (such as malloc, free, and all other shared functions).

3) If i compile my code with -fPIC, does it matter which part of memory I copy the .text chunk to?

My makefile is like this:
Code:
CC = arm-elf-gcc
LD = arm-elf-gcc
AS = arm-elf-as
AR = arm-elf-ar

PRG  = hello
OBJS = hello.o

LIBS      = -L../lib -lmirkoSDK -laddon -lm -lelf
CRT0      = ../lib/crt0.S
LNKSCRIPT = ../lib/lnkscript
INCLUDES  = -I../include
CFLAGS    = $(INCLUDES) -O3 -s -Wall -mtune=arm9tdmi -fPIC

all:	$(OBJS)
	$(CC) -c -o crt0.o $(CRT0)
	$(LD) -nostartfiles -s -Wall -Wl,-Map,Test.map  -T $(LNKSCRIPT) crt0.o -o $(PRG).elf $(OBJS) $(LIBS)

clean:
	rm -f *.o *~ Test.map *.bin


Ahh, you want to create an elf file, with dependencies to some functions already in the ram. Like linking my GPL lib to some LGPL stuff :)

Its not that hard to do this.
I do this with my psp linked stuff now too.

One Hint, have a closer look to the crt0.s

cu,
 
Last edited by a moderator:
Hi Mr.Mirko,

Yes, I want to be able to link to functions in RAM at runtime, but not specifically linking LGPL to your lib. More for sort of plugin/DLL support for my apps.

I had a look in gp32_crt0.s but i don't know much about what it's doing... obviously it is describing the different elf sections somehow (.txt .data etc) but apart from that I have no clue:

Code:
    .section	".init"
	.code 32
	.align
	.global _start
@---------------------------------------------------------------------------------
_start:
@---------------------------------------------------------------------------------
	b  _start2

@---------------------------------------------------------------------------------
@ AXF addresses
@---------------------------------------------------------------------------------
_text_start:
	.word   __text_start
_ro_end:
	.word   __ro_end
_data_start:
	.word   __data_start
	.word   __bss_end
_bss_start:
	.word   __bss_start
_bss_end:
	.word   __bss_end

@---------------------------------------------------------------------------------
@ GamePark magic sequence
@---------------------------------------------------------------------------------
	.word   0x44450011
	.word   0x44450011
	.word   0x01234567
	.word   0x12345678
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012

@---------------------------------------------------------------------------------
_start2:
@---------------------------------------------------------------------------------
	mrs  r0, CPSR
	orr  r0, r0, #0xC0
	msr  CPSR_ctl, r0

	mrs  r0, CPSR
	bic  r0, r0, #0xC0
	orr  r0, r0, #0x40
	msr  CPSR_ctl,r0

@---------------------------------------------------------------------------------
@ global constructors
@---------------------------------------------------------------------------------
	ldr  r3,=_call_main
	mov  lr,r3
	ldr  r3,=_init
	bx  r3
@---------------------------------------------------------------------------------
@ Jump to user code
@---------------------------------------------------------------------------------
_call_main:
@---------------------------------------------------------------------------------
	mov  lr, #0
	ldr  r3, =main
	bx  r3

	.pool
	.end

Do you have any actual code that i can have a look at such as linking/resolving symbols at runtime with functions in Ram?
 
Hi Mr.Mirko,

Yes, I want to be able to link to functions in RAM at runtime, but not specifically linking LGPL to your lib. More for sort of plugin/DLL support for my apps.

I had a look in gp32_crt0.s but i don't know much about what it's doing... obviously it is describing the different elf sections somehow (.txt .data etc) but apart from that I have no clue:

Code:
    .section	".init"
	.code 32
	.align
	.global _start
@---------------------------------------------------------------------------------
_start:
@---------------------------------------------------------------------------------
	b  _start2

@---------------------------------------------------------------------------------
@ AXF addresses
@---------------------------------------------------------------------------------
_text_start:
	.word   __text_start
_ro_end:
	.word   __ro_end
_data_start:
	.word   __data_start
	.word   __bss_end
_bss_start:
	.word   __bss_start
_bss_end:
	.word   __bss_end

@---------------------------------------------------------------------------------
@ GamePark magic sequence
@---------------------------------------------------------------------------------
	.word   0x44450011
	.word   0x44450011
	.word   0x01234567
	.word   0x12345678
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012
	.word   0x23456789
	.word   0x34567890
	.word   0x45678901
	.word   0x56789012

@---------------------------------------------------------------------------------
_start2:
@---------------------------------------------------------------------------------
	mrs  r0, CPSR
	orr  r0, r0, #0xC0
	msr  CPSR_ctl, r0

	mrs  r0, CPSR
	bic  r0, r0, #0xC0
	orr  r0, r0, #0x40
	msr  CPSR_ctl,r0

@---------------------------------------------------------------------------------
@ global constructors
@---------------------------------------------------------------------------------
	ldr  r3,=_call_main
	mov  lr,r3
	ldr  r3,=_init
	bx  r3
@---------------------------------------------------------------------------------
@ Jump to user code
@---------------------------------------------------------------------------------
_call_main:
@---------------------------------------------------------------------------------
	mov  lr, #0
	ldr  r3, =main
	bx  r3

	.pool
	.end

Do you have any actual code that i can have a look at such as linking/resolving symbols at runtime with functions in Ram?

I do no longer public sourcecode, for copy&paste purpose. So,
come one Pea, only TWO more lines in the crt0.s , and the symbol is resolved.
 
Last edited by a moderator:
I have absolutely no idea what you are talking about.

I have been reading up about ELF file format, and now have a moderate understanding. There is a tiny bit of info available about how to resolve symbols from a loaded shared library back into the main code.

For example if the library contains a reference to printf, this can be resolved back to the printf that is already in memory - though I still don't know how to how to look it up in memory..

The problem is how do i look up the address of a symbol in the shard library? E.g. a function... similar to the dlsym( library, symbol ) call?
 
Back
Top